如何使用java代码将上传的文件移动到存档文件夹

时间:2015-05-28 17:02:16

标签: java

public class Upload extends HttpServlet 
{

       private static final long serialVersionUID = 1L;
       public void init() throws ServletException
       {

               System.out.println(this.getClass().getName());
       }

       protected  void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
               //boolean MultipartRequest;
       //String PrintWriter;

               response.setContentType("text/html");

               PrintWriter out=response.getWriter();
               MultipartRequest multipartRequest = new MultipartRequest(request,"/home/hadoop/Desktop");

              out.println("succcesfully uploaded");

       }
       public void  destroy()
       {
               System.out.println(this.getClass().getName());
       }

}
  • 我需要做什么来将文件上传到Desktop文件夹 直接而不是直接给出文件名,如下所示
  • 还有一个是如何复制重复...
public class MoveFile {

    public static void main(String[] args) throws IOException {

        File sourceFile = new File("/home/hadoop/Desktop/LINUX-KACPER.pdf");
        File destinationDir = new File("/home/hadoop/Documents/movedfiles/");

        FileUtils.moveFileToDirectory(sourceFile, destinationDir, true);

    }

2 个答案:

答案 0 :(得分:3)

这是带有timemill的移动文件......

public class Moveall {
public static void main(String[] args) throws IOException {
    File destinationDir = new File("/home/hadoop/Documents/movedfiles/");
    File directory = new File("/home/hadoop/Desktop/");
    File[] subfiles = directory.listFiles();
    for(File f:subfiles){
    FileUtils.moveFile(f,new File(destinationDir.getPath()+"/"+System.currentTimeMillis()+f.getName()));
    System.out.println("moved succesfully....");

    }
}

}

答案 1 :(得分:1)

我不知道,这是答案:

public class MoveFile {
    public static void main(String[] args) throws IOException {
        File destinationDir = new File("/home/hadoop/Documents/movedfiles/");
        File directrory = new File("/home/hadoop/Desktop/");
        File[] subfiles = directory.listFiles();
        for(File f:subfiles){
            new File(destinationDir.getAbsolutePath()+f.getName()).delete() //remove the duplicate
            FileUtils.moveFileToDirectory(f, destinationDir, true);
        }
    }
}

此代码将选择桌面上的所有文件并将其移动到文件夹 您可以使用FileFilter作为listFiles的参数 http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles(java.io.FileFilter)

编辑(版本):

public class MoveFile {
    public static void main(String[] args) throws IOException {
        File destinationDir = new File("/home/hadoop/Documents/movedfiles/");
        File directrory = new File("/home/hadoop/Desktop/");
        File[] subfiles = directory.listFiles();
        for(File f:subfiles){
            File previous = new File(destinationDir.getAbsolutePath()+f.getName())
            if(!previous.exists()){
                FileUtils.moveFileToDirectory(f, destinationDir, true);
            }else{
                FileUtils.moveFile(f,new File(destinationDir.getAbsolutePath()+"_"+System.currentTimeMillis()+f.getName()), true);
            }
        }
    }
}

注意:由javadoc编写,未经测试。
方法
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()
首先是你如何制作版本,肯定可以通过
解决 http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getInstance()
但这只是一个简单的例子