`
zcw_java
  • 浏览: 303724 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

小白师傅讲解文件上传

 
阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics