当前位置:首页 > 开发 > 编程语言 > Java > 正文

使用java实现下载文件

发表于: 2015-03-18   作者:843977358   来源:转载   浏览:
摘要: 方法一: /** * 下载附件 * @param response * @throws IOException * @author zhangyd-c */ @RequestMapping(value="/downloadAccessory") public void downloadAccess
方法一:
/**
     * 下载附件
     * @param response
     * @throws IOException 
     * @author zhangyd-c
     */
    @RequestMapping(value="/downloadAccessory")
	public void downloadAccessory(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException
		{
    		request.setCharacterEncoding("utf8");
    		//获取项目真实路径
			String ctxPath = (new StringBuilder(String.valueOf(request.getSession().getServletContext().getRealPath("/")))).append("unstandard_materials/").toString();
			//获取文件的真实路径
			String downLoadPath = (new StringBuilder(String.valueOf(ctxPath))).append(fileName).toString();
			File files = null;
			InputStream fis = null;
			OutputStream os = null;
			try {
				//获取文件
				files = new File(downLoadPath);
				//读取该文件输入流到缓存
				fis = new BufferedInputStream(new FileInputStream(downLoadPath));
				/*
				 * fis.available():返回输入流中估计的字节数(输入流方法的下一次调用的剩余字节数)。
				 */
				byte buffer[] = new byte[fis.available()];
				//按字节读取缓存
				fis.read(buffer);
				response.reset();
				response.addHeader("Content-Disposition", (new StringBuilder("attachment;filename=")).append(new String(fileName.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1")).toString());
				response.addHeader("Content-Length", (new StringBuilder()).append(files.length()).toString());
				os = new BufferedOutputStream(response.getOutputStream());
				response.setContentType("application/octet-stream");
				//将字节数组写入输出流
				os.write(buffer);
				os.flush();
			} catch (FileNotFoundException e) {
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().write("服务器上不存在该附件(已丢失)!请联系管理员!");
			} catch (IOException e) {
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().write("服务器异常!请联系管理员!");
			}finally{
				if(fis != null){
					fis.close();
				}
				if(os != null){
					os.close();
				}
			}
		}

 方法二:

/**
     * 下载附件
     * @param response
     * @throws IOException 
     */
    @RequestMapping(value="/downloadAccessory")
	public void downloadAccessory(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException
		{
    		request.setCharacterEncoding("utf8");
    		response.setContentType("application/octet-stream; charset=utf-8");
    		//获取项目真实路径
			String ctxPath = (new StringBuilder(String.valueOf(request.getSession().getServletContext().getRealPath("/")))).
					append("unstandard_materials/").toString();
			//获取文件的真实路径
			String downLoadPath = (new StringBuilder(String.valueOf(ctxPath))).append(fileName).toString();
			OutputStream out = null;
			 try {
				response.reset();
				 String na = new String(fileName.getBytes("gbk"), "iso8859-1");
				 response.setHeader("Content-Disposition", "attachment; filename="+na);
				 out = response.getOutputStream();
				 out.write(FileUtils.readFileToByteArray(new File(downLoadPath)));
				 out.flush();
			}   catch (IOException e) {
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().write("服务器异常!请联系管理员!");
			}finally{
				if(out != null){
					out.close();
				}
			}
			
		}

 

使用java实现下载文件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号