1、因为我使用的是wicket1.4.22这个版本的上传组件不支持一次性选多个文件。所以我发现了ServletFileUpload这个类。但是我在使用的时候,不停的出现流意外关闭的情况。
public class Chen extends WebPage {
private MyFileUploadField fileUpload;
private String UPLOAD_FOLDER = "C:\\a\\";
private static Logger log = LoggerFactory.getLogger(Chen.class);
public Chen() {
add(new FeedbackPanel("feedback"));
Form<?> form = new Form<Void>("form") {
@Override
protected void onSubmit() {
final FileUpload uploadedFile = fileUpload.getFileUpload();
HttpServletRequest request =
((WebRequestCycle) RequestCycle.get()).getWebRequest().getHttpServletRequest();
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
factory.setRepository(new File(UPLOAD_FOLDER));
// 2、创建文件上传的核心类
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置上传文件的名称的编码
upload.setHeaderEncoding("UTF-8");
// ServletFileUpload的API
boolean multipartContent = upload.isMultipartContent(request);
log.info("111111111111111111111111111111");
log.info("111111111111111111111111111111");
log.info("aaa:"+request);
log.info("bbbb:"+upload);
// 根据 enctype="multipart/form-data" 判断表单是否是文件上传的表单
if (multipartContent) {
log.info("ccccc");
// 是文件上传的表单
// ***解析request获得文件项集合
List<FileItem> parseRequest = upload.parseRequest(request);
if (parseRequest != null) {
for (FileItem item : parseRequest) {
// 判断是不是一个普通表单项
boolean formField = item.isFormField();
if (formField) {
// username=zhangsan
String fieldName = item.getFieldName();
String fieldValue = item.getString("UTF-8");// 对普通表单项的内容进行编码
System.out.println(fieldName + "----" + fieldValue);
} else {
// 文件上传项
// 文件的名
String fileName = item.getName();
// 获得上传文件的内容
InputStream in = item.getInputStream();
OutputStream out = new FileOutputStream(UPLOAD_FOLDER + "/" + fileName);
//代替经典的文件拷贝
IOUtils.copy(in, out);
in.close();
out.close();
// 删除临时文件
item.delete();
}
}
}
} else {
log.info("dddddddd");
}
} catch (Exception e) {
log.error(e.toString());
log.error("error");
}
}
};
form.setMultiPart(true);
form.add(fileUpload = new MyFileUploadField("fileUpload"));
add(form);
}
}
List parseRequest = upload.parseRequest(request);
我获取到的是一个空。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/2002/08/xhtml/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
<head>
<style>
.feedbackPanelINFO {
color: green;
}
.feedbackPanelERROR {
color: red;
}
</style>
</head>
<body>
<h1>upload dicom file</h1>
<div wicket:id="feedback"></div>
<form wicket:id="form" method="post" enctype="multipart/form-data">
<input type="text" name="username"><br>
<input type="file" name="filename"><br>
<input type="submit" value="上传文件">
</form>
</body>
</html>