首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

小白师傅讲解文件下传

2012-12-27 
小白师傅讲解文件上传SpringMVC用的不太好,小白师傅是高人!!先共享代码下载private void outputFile(File

小白师傅讲解文件上传
SpringMVC用的不太好,小白师傅是高人!!
先共享代码


下载
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

热点排行