Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在GWT中,是否保证按表单上的顺序发送多部分表单数据?_Java_File Upload_Gwt_Multipartform Data_Formpanel - Fatal编程技术网

Java 在GWT中,是否保证按表单上的顺序发送多部分表单数据?

Java 在GWT中,是否保证按表单上的顺序发送多部分表单数据?,java,file-upload,gwt,multipartform-data,formpanel,Java,File Upload,Gwt,Multipartform Data,Formpanel,我用FormPanel和三个FileUpload对象构建了一个表单。三个FileUpload对象指的是不同类型的二进制文件。 我已经做了很多测试,文件总是按照我在三个FileUpload对象中添加它们的顺序(从上到下)放置在列表中。 例如,在下面的表格中,文件1、2和3按顺序到达服务器(我已经用各种文件运行了20或30次): 这能保证吗?或者我应该找到一种方法来标记它们吗?使用FileItemIterator时,您可以检查表单上的每个项目。据我所知,它们是按HTML中的顺序出现的 迭代器将让您

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


这能保证吗?或者我应该找到一种方法来标记它们吗?

使用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>

最大尺寸
3145728
最大文件大小
1024000
懒虫
200
文件上传
com.parity.mediamanager.server.FileUploadServlet
文件上传
/文件上传

我知道servlet设置可以工作,但我仍然不确定上下文参数以及它们是否真的起作用。

不确定顺序,但您可以在使用FileItemIterator遍历字段时检查字段名,以便知道正在处理哪个字段。您还可以让文件选择器选择多个文件,而不是每个小部件选择一个文件。虽然这样说,但上次我使用它时,我在文件小部件之前已经隐藏了解析过的字段。因此,使用上面的示例,字段名将给出“文件1”等。。如果我这样标记
文件上传
?我事先不知道这些文件的名称,它们都是同一类型的。是的,没错。getFieldName()返回字段名,getName()返回上传文件名。这对我来说已经足够好了!它解决了我的问题。是否要添加它作为答案?