2014-10-28 58 views
0

我得到這個錯誤,當我上傳到zipfile文件。Primefaces文件上傳 - ZipFile錯誤

錯誤:Error creating zip file: java.io.FileNotFoundException: C:\fupload\qhT39xmU- (The system cannot find the file specified)

這是我上傳的方法:

public String uploadToFts(UploadedFile filem, String fileType){ 
    String fileFtsUrl = null; 
    String uploadUrl = fileDAO.findByUniqueProperty("name", "geturl").getPropValue(); 
    String usr= fileDAO.findByUniqueProperty("name", "usr").getPropValue(); 
    String pwd= fileDAO.findByUniqueProperty("name", "pwd").getPropValue(); 
    String nameId= String.valueOf(passGen.create(1, 1, 8, 8, 0)) + "-"; 
    try{ 
     ClientConfig cc = new DefaultClientConfig(); 
     Client client = Client.create(cc); 
     client.addFilter(new HTTPBasicAuthFilter(usr, pwd)); 
     try { 
      FormDataMultiPart form = new FormDataMultiPart(); 
      File file = new File("C:/fupload/"+nameId+""); 
      File thumbnail = new File("C:/fupload/"+nameId+"-tmb.jpg"); 
      zipFile("C:/fupload/"+nameId+".zip", file, thumbnail); 
      File zipFile = new File("C:/fupload/"+nameId+".zip"); 
      String urlParams = "nameId=" + nameId+ "&" + 
        "fileType="+fileType+"&" + 
        "fileName=" + zipFile.getName() + "&" + 
        "zipped=true"; 
      form.bodyPart(new FileDataBodyPart("file", zipFile, MediaType.MULTIPART_FORM_DATA_TYPE)); 
      WebResource resource = client.resource(uploadUrl + urlParams); 
      ClientResponse response = resource.type(MediaType.APPLICATION_OCTET_STREAM).put(ClientResponse.class, zipFile); 
      String respStr = response.getEntity(String.class); 
      if(respStr.contains("\"status\":0")){ 
       System.out.println(respStr); 
       JSONObject obj = new JSONObject(respStr); 
       int jsonStatus = obj.getInt("status"); 
       int jsonTxnId = obj.getInt("txnid"); 
       String url = obj.getString("url"); 

       fileFtsUrl = url; 
      } else { 
       addMessageToView(FacesMessage.SEVERITY_ERROR, "", "Can't uploading FTS"+respStr); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return fileFtsUrl; 
    } catch (Exception e){ 
     e.printStackTrace(); 
     return fileFtsUrl; 
    } 
} 

ZipFile的方法

public static void zipFile(String zipFile, File... srcFiles) { 
    try { 
     // create byte buffer 
     byte[] buffer = new byte[1024]; 
     FileOutputStream fos = new FileOutputStream(zipFile); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     for (int i = 0; i < srcFiles.length; i++) { 
      File srcFile = srcFiles[i]; 
      FileInputStream fis = new FileInputStream(srcFile); 
      // begin writing a new ZIP entry, positions the stream to the start of the entry data 
      zos.putNextEntry(new ZipEntry(srcFile.getName())); 
      int length; 
      while ((length = fis.read(buffer)) > 0) { 
       zos.write(buffer, 0, length); 
      } 
      zos.closeEntry(); 
      // close the InputStream 
      fis.close(); 
     } 
     // close the ZipOutputStream 
     zos.close(); 
    } catch (IOException ioe) { 
     System.out.println("Error creating zip file: " + ioe); 
    } 
} 

回答

0

我想你只需要創建壓縮文件的文件(..),因爲它不會存在:

File file = new File(zipFile); 
if(!file.exists()) { 
    file.createNewFile(); 
} 
FileOutputStream fos = new FileOutputStream(file, false); 

或者,也許更能之前創建它,並把它傳遞給壓縮文件(文件,...),所以你不必只是調用後重新創建。所以通過一個File而不是String與其他參數一樣。

0

請與

System.out.println("C:/fupload/"+nameId+"") 

System.out.println("C:/fupload/"+nameId+"-tmb.jpg") 

在你的系統是存在嗎?

確保您的路徑必須在您的系統中指向文件