服务器上 Struts 的 FileUpload 问题

发布于 2024-09-27 10:16:48 字数 1811 浏览 6 评论 0原文

我正在尝试创建一个处理表单中的 enctype="multipart/form-data" 的上传 servlet。我尝试上传的文件是一个 zip 文件。但是,我可以在本地主机上上传并读取文件,但是当我上传到服务器时,当我想上传文件时,出现“找不到文件”错误。这是由于我使用的 Struts 框架造成的吗?感谢您的帮助。这是我的代码的一部分,我正在使用 http://commons.apache.org/fileupload 中的 FileUpload /using.html

我已更改为使用ZipInputStream,但是,如何在不使用本地磁盘地址(即:C://zipfile.zip)的情况下引用ZipFile zip。 zip 为 null,因为它未实例化。我需要解压缩并读取内存中的 zipentry,而不写入服务器。

对于上传 servlet: > 私人 ZipFile zip; 私人 CSVReader 阅读器; boolean isMultipart = ServletFileUpload.isMultipartContent(request); 如果(是多部分){ DiskFileItemFactory 工厂 = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);
       List <FileItem> items = upload.parseRequest(request);
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            //Iterating through the uploaded zip file and reading the content
            FileItem item = (FileItem) iter.next();

             ZipInputStream input = new ZipInputStream(item.getInputStream()); 
             ZipEntry entry = null;
             while (( entry= input.getNextEntry()) != null) {
               ZipEntry entry = (ZipEntry) e.nextElement();
               if(entry.getName().toString().equals("file.csv")){
                   //unzip(entry)
               }

               }
            }


  public static void unzip(ZipEntry entry){
        try{
            InputStream inputStream = **zip**.getInputStream(entry);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            reader = new CSVReader(inputStreamReader);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

<

I am trying to create a upload servlet that handles enctype="multipart/form-data" from a form. The file I am trying to upload is a zip. However, I can upload and read the file on localhost, but when I upload to the server, I get a "File not found" error when I want to upload a file. Is this due to the Struts framework that I am using? Thanks for your help. Here is part of my code, I am using FileUpload from http://commons.apache.org/fileupload/using.html

I have changed to using ZipInputStream, however, how to I reference to the ZipFile zip without using a local disk address (ie: C://zipfile.zip). zip is null because its not instantiated. I will need to unzip and read the zipentry in memory, without writing to the server.

For the upload servlet:
>
private ZipFile zip;
private CSVReader reader;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
DiskFileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);
       List <FileItem> items = upload.parseRequest(request);
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            //Iterating through the uploaded zip file and reading the content
            FileItem item = (FileItem) iter.next();

             ZipInputStream input = new ZipInputStream(item.getInputStream()); 
             ZipEntry entry = null;
             while (( entry= input.getNextEntry()) != null) {
               ZipEntry entry = (ZipEntry) e.nextElement();
               if(entry.getName().toString().equals("file.csv")){
                   //unzip(entry)
               }

               }
            }


  public static void unzip(ZipEntry entry){
        try{
            InputStream inputStream = **zip**.getInputStream(entry);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            reader = new CSVReader(inputStreamReader);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

<

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-10-04 10:16:55

在这里,

zip = new ZipFile(new File(fileName));

您假设服务器计算机上的本地磁盘文件系统已经包含与客户端名称完全相同的文件。这是一个错误的假设。它在本地主机上工作显然是因为网络浏览器和网络服务器“巧合”地在物理上运行在具有相同磁盘文件系统的同一台机器上。

此外,您似乎使用 Internet Explorer 作为浏览器,它错误地在文件名中包含完整路径,例如 C:/full/path/to/file.ext。您不应该依赖这个浏览器特定的错误。其他浏览器(例如 Firefox)仅正确发送文件名,例如 file.ext,这反过来会导致 new File(fileName) 失败(这应该可以帮助您更快地发现你的错误)。

要解决这个“问题”,您需要通过 item.getInputStream() 获取文件 contents 作为 InputStream

ZipInputStream input = new ZipInputStream(item.getInputStream());
// ...

或者将其写入磁盘通过 item.write(file) 并在 ZipFile 中引用它:

File file = File.createTempFile("temp", ".zip");
item.write(file);
ZipFile zipFile = new ZipFile(file);
// ...

注意:不要忘记事先检查文件扩展名,否则可能会卡住。

Here,

zip = new ZipFile(new File(fileName));

You're assuming that the local disk file system at the server machine already contains the file with exactly the same name as it is at the client side. This is a wrong assumption. That it worked at localhost is obviously because both the webbrowser and webserver "by coincidence" runs at physically the same machine with the same disk file system.

Also, you seem to be using Internet Explorer as browser which incorrectly includes the full path in the filename like C:/full/path/to/file.ext. You shouldn't be relying on this browser specific bug. Other browsers like Firefox correctly sends only the file name like file.ext, which in turn would have caused a failure with new File(fileName) (which should have helped you to spot your mistake much sooner).

To fix this "problem", you need to obtain the file contents as InputStream by item.getInputStream():

ZipInputStream input = new ZipInputStream(item.getInputStream());
// ...

Or to write it to disk by item.write(file) and reference it in ZipFile:

File file = File.createTempFile("temp", ".zip");
item.write(file);
ZipFile zipFile = new ZipFile(file);
// ...

Note: don't forget to check the file extension beforehand, else this may choke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文