2015-05-28 59 views
2
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()); 
     } 

} 
  • 什麼,我需要做的移動uploded到桌面文件夾 直接,而不是直接給文件名,如下
  • 和一個要移動的文件上傳文件到歸檔文件夾更多的是如何複製重複太...
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); 

    } 

回答

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

我不知道,這是答案:

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); 
     } 
    } 
} 

該代碼將選擇在桌面上的所有文件,並將其移動到movedfiles 可以使用的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()
解決但這僅僅是簡單的例子

+0

thnkü@maskacovnik這是工作,但我需要太複製重複值u能請幫助我 – nani

+0

我不知道你的意思是重複的值......,你的意思是重寫文件,或者你的意思是子文件夾?如果你的意思是重寫,只是刪除文件,新文件將被粘貼。我已更新我的源代碼,但它沒有測試@nani – maskacovnik

+0

對不起,重複的值其重複文件我的意思是兩個文件具有相同的名稱和數據 – nani