如何从FileItem获取完整路径

时间:2017-11-01 09:51:05

标签: java file file-upload upload zip

如何从FileItem获取完整路径并放入FileInputStream,似乎我只能getName。下面是我的代码,我试图上传文件和自动压缩到服务器。谢谢

DiskFileItemFactory fup = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fup);

List items = upload.parseRequest(req);
Iterator iter = items.iterator();

while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();                             
                if (!item.isFormField()) {

                File f = new File(Properties.get("dir") + File.separator + "test.zip");
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
                ZipEntry e = new ZipEntry(flNm);
                out.putNextEntry(e);
                FileInputStream in = new FileInputStream((File) item);

                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.closeEntry();

                out.close();
                }
}

1 个答案:

答案 0 :(得分:1)

您正在读取引用新创建的临时文件的File对象,该文件为空。并将其写入文件。你确定那是你想做的吗?