在 GWT 中,多部分表单数据是否保证按表单上的顺序发送?
In GWT, is Multipart form-data guaranteed to be sent in the order it is on the form?
我有一个用 FormPanel
和三个 FileUpload
对象构建的表单。三个 FileUpload
对象指的是不同类型的二进制文件。
我已经测试了很多,文件总是按照我在三个 FileUpload
对象中添加它们的顺序(从上到下)放置在列表中。
因此,例如,在下面的表格中,文件 1、2 和 3 按该顺序到达服务器(我已经 运行 它与各种文件 20 或 30 次):
那是 gua运行teed 吗?还是我应该想办法给它们贴上标签?
使用 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 设置有效,但我仍然不确定上下文参数以及它们是否真的有所作为。
我有一个用 FormPanel
和三个 FileUpload
对象构建的表单。三个 FileUpload
对象指的是不同类型的二进制文件。
我已经测试了很多,文件总是按照我在三个 FileUpload
对象中添加它们的顺序(从上到下)放置在列表中。
因此,例如,在下面的表格中,文件 1、2 和 3 按该顺序到达服务器(我已经 运行 它与各种文件 20 或 30 次):
那是 gua运行teed 吗?还是我应该想办法给它们贴上标签?
使用 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 设置有效,但我仍然不确定上下文参数以及它们是否真的有所作为。