2012-10-17 31 views
4

我想創建存在於一個ftp位置的文件的zip文件並將該zip文件複製到其他ftp位置而不在本地保存。壓縮存在於一個FTP位置並直接複製到另一個FTP位置的文件

我能夠處理該問題files.It的小尺寸很適合小尺寸的文件,1 MB等

但是如果文件大小大像100 MB,200 MB,300 MB那麼它給錯誤如,

java.io.FileNotFoundException: STOR myfile.zip : 550 The process cannot access the 
file because it is being used by another process. 
at sun.net.ftp.FtpClient.readReply(FtpClient.java:251) 
at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:208) 
at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:398) 
at sun.net.ftp.FtpClient.put(FtpClient.java:609) 

我的代碼是

URLConnection urlConnection=null; 
    ZipOutputStream zipOutputStream=null; 
    InputStream inputStream = null; 
    byte[] buf; 
    int ByteRead,ByteWritten=0; 
    ***Destination where file will be zipped*** 
    URL url = new URL("ftp://" + ftpuser+ ":" + ftppass + "@"+ ftppass + "/" + 
      fileNameToStore + ";type=i"); 
    urlConnection=url.openConnection();   
    OutputStream outputStream = urlConnection.getOutputStream(); 
    zipOutputStream = new ZipOutputStream(outputStream); 
    buf = new byte[size]; 
    for (int i=0; i<li.size(); i++) 
    { 
     try 
     { 
      ***Souce from where file will be read*** 
      URL u= new URL((String)li.get(i)); // this li has values http://xyz.com/folder 
      /myPDF.pdf 
      URLConnection uCon = u.openConnection(); 
      inputStream = uCon.getInputStream(); 
      zipOutputStream.putNextEntry(new ZipEntry((String)li.get(i).substring((int)li.get(i).lastIndexOf("/")+1).trim())); 
      while ((ByteRead = inputStream .read(buf)) != -1)  
      {  
       zipOutputStream.write(buf, 0, ByteRead); 
       ByteWritten += ByteRead; 
      } 
      zipOutputStream.closeEntry(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    if (inputStream != null) { 
     try { 
      inputStream .close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    if (zipOutputStream != null) { 
     try { 
      zipOutputStream.close(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     }  
    } 

任何人都可以讓我知道我怎樣才能避免這種錯誤和處理大文件

回答

1

這與文件大小無關;如錯誤所述,您不能替換該文件,因爲其他進程當前正在鎖定它。

爲什麼你更常看到大文件的原因是因爲這些文件傳輸時間較長,因此併發訪問的機會較高。

因此,唯一的解決方案是確保在您嘗試傳輸文件時沒有人使用該文件。祝你好運。

可能的其他解決方案:

  1. 不要在服務器上使用Windows。
  2. 將文件傳輸到臨時名稱下,並在完成時對其進行重命名。這樣,其他進程將不會看到不完整的文件。總是一件好事。
  3. 使用rsync而不是再次發明輪子。
+0

你好@Aaron。如果您可以向我提供關於第2點和第3點的更多信息? – user1493286

+0

隨時發佈新的問題。只要確定你明確提到你需要知道/不明白的東西。 –

+0

@Aaron.Thanks.for point 2.最初,我創建目標zip文件名,然後迭代我的列表(我將添加到zip的文件)並將其添加到上面的目標zip中。我不明白你提出的這個重命名的東西。我可以如何實現這一點。另外我完全不理解你的觀點沒有3. – user1493286

相關問題