小白师傅讲解文件上传

SpringMVC用的不太好,小白师傅是高人!!
先共享代码

/**
* download 方法
* <p>方法说明:以数据流的方式下载附件</p>
* @param id 要下载的附件id
* @param response
* @throws IOException
* @return void
* @author xiaobai
* @date 2011-8-29
*/

@RequestMapping("/download/{id}")
@ResponseBody
public void download(@PathVariable Long id, HttpServletResponse response)
throws IOException {
response.reset();
UploadFile uf = uploadFileService.get(id);
if (uf == null) {
throw new BusinessException("您下载文件不存在");
}
File file = new File(uploadFileService.getRoot()+uf.getUrl());
if (file == null||!file.exists()) {
throw new BusinessException("您下载文件不存在");
}
try {
response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(uf.getName(),"utf-8"));
} catch (UnsupportedEncodingException e1) {
response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(uf.getName(),"utf-8"));
}
response.addHeader("Content-Length", String.valueOf(file.length()));
response.setContentType("bin");
try {

outputFile(file, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("文件下载错误");
} catch (RuntimeException e) {
e.printStackTrace();
throw new BusinessException("文件下载错误");
}
}


下载

@RequestMapping("/uploadFile/{id}")
@ResponseBody
public String uploadFile(@PathVariable Long id,
@RequestParam MultipartFile file) throws IOException {
Map<String, Object> modelMap = new HashMap<String, Object>();
uploadFileService.uploadFile(id, file);
modelMap.put("success", true);
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
return new ObjectMapper().filteredWriter(filterProvider)
.writeValueAsString(modelMap);
}



private void outputFile(File file, OutputStream outputStream) {
BufferedInputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[1024];
int len = -1;
while ((len = is.read(b, 0, b.length)) != -1) {
outputStream.write(b, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}
}
}
}


1,为什么要用@ResponseBody?
答:好处就是使用ajax,不用刷新.因为把返回值封装到response中了
2,前台就不用js写获取脚本,直接就无刷新吗?
答:当然不是,Spring默认有个convertor,会把你的map,list,object等转换成你想要的json格式,前台只需要接受这个json并解析输出到相应的dom上就行了.
3,没懂,我之前都是用out输出一个json
答:out?哪来.是response.writer?这个跟你用的原理是一样的,封装起来而已!
只要你return map,就是HashMap,return list,return obj;等就会把return的对象自动转换成json了
(MIME格式)
http://www.w3school.com.cn/media/media_mimeref.asp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值