您当前的位置: 首页 > 

小志的博客

暂无认证

  • 3浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

文件下载例子

小志的博客 发布时间:2018-12-10 11:14:20 ,浏览量:3

 /**
     * 检索列表中下载附件
     * 传入参数:@param uploadFile
     * 传入参数:@param request
     * 返回类型:void
     */
    @RequestMapping(value = "/downloadFile")
    public void downloadFile(UploadFile uploadFile,HttpServletRequest request, HttpServletResponse response) {
		BufferedInputStream in = null;  
		BufferedOutputStream out = null; 
		FileInputStream fis=null;
		try {  
			/*根据id单条查询*/
			uploadFile = allSearchService.queryFileInformationById(uploadFile);
			response.setContentType("text/html;charset=UTF-8");   
			//String rootpath = request.getSession().getServletContext().getRealPath("/");  
			String rootpath=SysParamInits.ARRANGE_FILE_PATH;//配置文件中指定下载文件路径。例D:\files
			File f = new File(rootpath + File.separator + uploadFile.getFilepath());  
			response.reset();
			response.setContentType("application/x-excel");  
			response.setCharacterEncoding("UTF-8");  
			response.setHeader("Content-Disposition", "attachment; filename=" 
			+ new String(uploadFile.getFilename().getBytes("gbk"),"iso-8859-1"));  
			response.setHeader("Content-Length",String.valueOf(f.length()));
			fis=new FileInputStream(f);
			in = new BufferedInputStream(fis);  
			out = new BufferedOutputStream(response.getOutputStream());  
			byte[] data = new byte[1024];  
			int len = 0;  
			while (-1 != (len=in.read(data, 0, data.length))) {  
				out.write(data, 0, len);  
			}  
		} catch (Exception e) {  
			e.printStackTrace();  
		} finally {  
			if (in != null) {  
				try {
					in.close();
				} catch (IOException e) {
				}  
			}  
			if (out != null) {  
				try {
					out.close();
				} catch (IOException e) {
				}  
			}  
			if (fis != null) {  
				try {
					fis.close();
				} catch (IOException e) {
				}  
			}  
		}  
    }
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.1521s