[英]Primefaces File Upload - ZipFile Error
當我使用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);
}
}
我認為您只需要在zipFile(..)中創建文件,因為它將不存在:
File file = new File(zipFile);
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file, false);
也許最好在之前創建它並將其傳遞給zipFile(file,...),這樣您就不必在調用之后再次創建它。 因此,與其他參數一樣,傳遞File
而不是String
。
檢查與
System.out.println("C:/fupload/"+nameId+"")
System.out.println("C:/fupload/"+nameId+"-tmb.jpg")
系統中是否存在?
確保您的路徑必須指向系統中的文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.