1、spring-mvc.xml配置
bean中的id必须为multipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"></property>
</bean>
2、表单上传(MVC接受)
@RequestMapping("/doupload")
public String doUpload(MultipartFile fileName,HttpSession session){
boolean falg = upload(fileName,session);
if(falg){
return "redirect:doSuccess.do";
}else{
return "redirect:doError.do";
}
}
private boolean upload(MultipartFile file,HttpSession session) {
boolean flag = false;
try{
String fileName = file.getOriginalFilename();
String sufName = fileName.substring(fileName.lastIndexOf("."));
String proName = UUID.randomUUID().toString();
String pathName = proName + sufName;
File f = new File("H:\\image\\upload\\"+pathName);
file.transferTo(f);
flag = true;
session.setAttribute("path", pathName);
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
3、ajax上传(MVC接受)
@RequestMapping("/doAjaxUpload")
@ResponseBody
public String doAjaxUpload(@RequestParam MultipartFile file){
boolean falg = upload(file);
System.out.println(falg);
if(falg){
return "success";
}else{
return "error";
}
}
private boolean upload(MultipartFile file) {
System.out.println("001");
boolean flag = false;
try{
String fileName = file.getOriginalFilename();
String sufName = fileName.substring(fileName.lastIndexOf("."));
String proName = UUID.randomUUID().toString();
String fileImgName = proName + sufName;
File f = new File("H:\\image\\upload\\"+fileImgName);
file.transferTo(f);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
4、ajax上传 两种方式
//ajax表单上传 第一种
function doUpload(){
$("#uploadFormId").ajaxSubmit({
type:"post",
url:"attachements/insertAttachement",
dataType:"json",
success:function(result){
alert(result.message);
findAttachments();
}
});
return false;//防止表单重复提交的一种方式
}
//ajax表单上传 第二种
$("#btn").click(function(){
alert("click");
var formData = new FormData($("#form")[0]);
$.ajax({
url:"${pageContext.request.contextPath }/ajax/doAjaxUpload.do",
data:formData,
cache:false,
contentType:false,//告诉jQuery不要去设置请求头
processData:false,//告诉spring不要去处理发送的数据
success:function(data){
if(data=="success"){
alert("上传成功!!!");
}
}
})
})
mvc文件下载
@RequestMapping("/download")
public ResponseEntity<byte[]> download(Integer id) throws Exception{
//获取Attachements对象
Attachements att =attachementsService.findAttById(id);
byte[] by;
FileInputStream in = new FileInputStream(att.getFilePath());//得到文件路径 用IO流的形式将文件已字节形式存放到by字节数组中
by = new byte[in.available()];
in.read(by);
//进行编码与解码
String fileName = new String(att.getFileName().getBytes(),"ISO8859-1");
//设置请求头
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;fileName="+fileName);
ResponseEntity<byte[]> entity = new ResponseEntity<>(by, headers, HttpStatus.OK);
return entity;
}
需要导入的依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
本文详细介绍如何在Spring MVC框架中实现文件上传和下载功能,包括配置multipartResolver、使用MultipartFile处理上传,以及通过ResponseEntity实现文件下载。同时,文章还提供了两种AJAX上传方法的示例代码。
1064

被折叠的 条评论
为什么被折叠?



