简体   繁体   English

Primefaces文件上传-ZipFile错误

[英]Primefaces File Upload - ZipFile Error

I m getting this Error, when I upload to file with zipfile. 当我使用zipfile上传到文件时,出现此错误。

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

This is My Upload Method : 这是我的上传方法:

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

I think you just need to create the file in zipFile(..), as it won't exist: 我认为您只需要在zipFile(..)中创建文件,因为它将不存在:

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

Or maybe better to create it before and pass it to zipFile(file, ...), so you won't have to create it again just after the call. 也许最好在之前创建它并将其传递给zipFile(file,...),这样您就不必在调用之后再次创建它。 So pass a File instead of a String as with the other arguments. 因此,与其他参数一样,传递File而不是String

Check with 检查与

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

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

Is exist in your system? 系统中是否存在?

Make sure your path is must point file in your system 确保您的路径必须指向系统中的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM