2014-10-28 57 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") 

在你的系统是存在吗?

确保您的路径必须在您的系统中指向文件