form设置了enctype="multipart/form-data" 属性后提交表单报错
发布网友
发布时间:2022-04-21 13:22
我来回答
共1个回答
热心网友
时间:2022-04-21 14:52
HashMap<String,String> map=new HashMap<String,String>();
String displayImage = "";
// 采用apache工具包进行文件上传操作
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析 请求信息
List<FileItem> fileitems = upload.parseRequest(request);
for (FileItem item : fileitems) {
if (item.isFormField()) {
// 简单表单信息处理
String name = item.getFieldName();
String value = item.getString();
// 转换下字符集编码
value = new String(value.getBytes("iso-8859-1"), "utf-8");
map.put(name, value);
} else {
// 原文件名
String filename = item.getName();
if(filename.length()>0){
String dotName = "";
int index = filename.lastIndexOf('.') ;
if(index > 0){
dotName = filename.substring(index);
}
// 生成新的文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
filename = sdf.format(new Date()) + dotName;
displayImage = "UploadFile/" + filename;
String dir = application.getRealPath("UploadFile");
System.out.print(dir);
item.write(new File(dir, filename));
// 删除临时文件
item.delete();
}
}
}