我有一个应该处理上传文件的 servlet。Apache 通用文件上传库用于此目的。
问题是文件损坏了。看起来符号“~”被替换为“?” 在文件中。
这里我的html表单是:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<FORM action="http://localhost:8081/wihome-connector-bpm/bpmFileUpload"
enctype="multipart/form-data"
method="post">
What files are you sending?
<INPUT type="file" name="uploadedFiles"><BR>
<INPUT type="submit" value="Send">
<INPUT type="reset">
</FORM>
</body>
</html>
这就是 servlet:
public class FileUploadServlet extends HttpServlet {
private static final Log LOG = LogFactory.getLog(FileUploadServlet.class);
/**
* {@inheritDoc}
*/
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
LOG.info("Uploading file: " + item.getName());
byte[] fileContents = IOUtils.toByteArray(item.getInputStream());
//...
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我正在尝试上传图片
但相反,我明白了:
你能帮我么?可能是什么问题?