response.setContentType("text/html; charset=UTF-8");
Boolean isMultipart = ServletFileUpload.isMultipartContent(request);
PrintWriter out = response.getWriter();
if (isMultipart) {
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
servletFileUpload.setHeaderEncoding("UTF-8");//解决http报头乱码,即中文文件名乱码
try {
List<FileItem> list = servletFileUpload.parseRequest(request);
for (Iterator<FileItem> iter = list.iterator(); iter.hasNext();) {
FileItem item = iter.next();
if (item.isFormField()) {
out.println(item.getFieldName());
out.println("<br/>");
out.println("<br/>");
out.println(item.getString("UTF-8")); //解决表单输入输乱码。
}
else{
out.println(item.getFieldName());
out.println(item.getName());
}
}
} catch (Exception e) {
//throw new BusinessException(" 出现文件上传错误。 ");
System.out.println(" 出现文件上传错误。 ");
}
}
commons-fileUpload组件解决文件上传乱码问题
最新推荐文章于 2024-09-06 13:04:27 发布