废话不多说,上代码:
jsp页面:
<form action="fileUpload1.do" enctype="multipart/form-data"
method="post">
<div style="margin-bottom: 20px">
<div>File1:</div>
<input class="easyui-filebox" name="sourceFile"
data-options="prompt:'Choose a file...'" style="width: 20%">
</div>
<input type="submit" value="上传">
</form>
控制层springmvc接收并处理:
public String fileUpload(FileUpload file) {
MultipartFile multipartFiles = file.getSourceFile();
if (multipartFiles != null) {
try {
byte[] by = multipartFiles.getBytes();
WriteToDiskImpl.writeToDiskBytes(by, "e://abc.txt");
} catch (IOException e) {
try {
throw new CustomException("读取文件失败");
} catch (CustomException e1) {
e1.printStackTrace();
}
}
}
System.out.println("111");
return "sucess";
}
FileUpload 的pojo
public class FileUpload implements Serializable {
private Integer id;
private String name;
private String description;
private String url;
private String icon;
private MultipartFile sourceFile;
setget().....方法
}
具体操作写的方法
@Override
public void writeToDiskBytes(byte[] bytes, String path) throws IOException {
FileOutputStream fo = new FileOutputStream(path);
fo.write(bytes);
fo.close();
}
需要注意几点:
1.必须以Post方式提交
2.文件类型为: multipart/form-data
3.jsp页面上传文件的input 的name与pojo的属性保持一致