1

我有一个用FormPanel三个FileUpload对象构建的表单。这三个FileUpload对象指的是不同类型的二进制文件。FileUpload我已经测试了很多,并且文件始终按照我在三个对象中添加它们的顺序(从上到下)放置在列表中。因此,例如,在下面的表格中,文件 1、2 和 3 以该顺序到达服务器(我已经使用各种文件运行了 20 或 30 次):

文件上传示例

这有保证吗?还是我应该想办法给它们贴上标签?

4

1 回答 1

1

使用 FileItemIterator 时,您可以检查表单上的每个项目。据我所知,它们确实按顺序排列在 HTML 中。

迭代器会让您知道它是表单字段还是上传文件,如我编写的旧函数所示。

在处理文件上传时,使用 getFieldName() 来识别表单字段并使用 getName() 来处理来自客户端的文件名。

困难在于在 web.xml 文件的参数中分配 servlet。

希望下面的代码能帮助你弄清楚。

public class FileUploadServlet extends HttpServlet {

    private static final long serialVersionUID = -6988332690679764038L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws IOException {

        String path = "/home/tomcat/engage/media/";
        String user = "";

        if (ServletFileUpload.isMultipartContent(request)) {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            boolean gotPath = false;

            String message = "";
            String media_category = "";

            StringBuilder sb = new StringBuilder();
            sb.append(Shared.getTimeStamp() + ": Uploading med files to category - ");

            try {
                FileItemIterator it = upload.getItemIterator(request);

                while (it.hasNext()) {
                    FileItemStream item = it.next();

                    //message += item.getFieldName() + ": ";

                    if (item.isFormField()) {
                        if (item.getFieldName().equals("MediaCategory")) {
                            media_category = Streams.asString(item.openStream());
                            path += media_category;

                            gotPath = true;

                            message += path + System.lineSeparator();

                        } else if (item.getFieldName().equals("UserName")) {

                            user += Streams.asString(item.openStream());

                        }
                    } else {
                        if (gotPath) {
                            String fileout = path + "/" + item.getName();

                            message += fileout + System.lineSeparator();

                            InputStream input = null;
                            OutputStream output = null;

                            try {
                                output = new FileOutputStream(new File(fileout));

                                input = item.openStream();

                                byte[] buffer = new byte[256];
                                int count;

                                while ((count = input.read(buffer)) > 0) {
                                    output.write(buffer, 0, count);
                                }

                            } finally {
                                input.close();
                                output.close();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                response.sendRedirect("Error on item: " + e.getLocalizedMessage());
            }

            response.setStatus(HttpServletResponse.SC_CREATED);

            //response.getWriter().print(message);

            sb.append(message + System.lineSeparator());
            Shared.writeUserLog(user, sb.toString());

        } else {
            response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, 
                    "Unsupported media type...");
        }

    }
}

web.xml

<context-param>
    <!-- max size of the upload request -->
    <param-name>maxSize</param-name>
    <param-value>3145728</param-value>
</context-param>
<context-param>
    <!-- max size of any uploaded file -->
    <param-name>maxFileSize</param-name>
    <param-value>1024000</param-value>
</context-param>
<context-param>
    <!-- Useful in development mode to slow down the uploads in fast networks. 
        Put the number of milliseconds to sleep in each block received in the server. 
        false or 0, means don't use slow uploads -->
    <param-name>slowUploads</param-name>
    <param-value>200</param-value>
</context-param>

<servlet>
    <servlet-name>fileUpload</servlet-name>
    <servlet-class>com.parity.mediamanager.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>fileUpload</servlet-name>
    <url-pattern>/fileupload</url-pattern>
</servlet-mapping>

我知道 servlet 设置有效,但我仍然不确定上下文参数以及它们是否真的有所作为。

于 2017-10-03T09:21:22.133 回答