`

FileDowload

阅读更多
package com.piend.rjt.server.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.piend.rjt.server.util.IfoContent;
import com.piend.rjt.server.util.JsonUtil;
import com.piend.rjt.server.util.StringUtils;

public class FileDowload extends HttpServlet {
	private static final long serialVersionUID = 1435996665443858685L;
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = "rjt_backup/";
		String id = req.getParameter("id");
		String userCenter = req.getParameter("userCenter");
		String fileName= req.getParameter("fileName");
		if(StringUtils.isEmty(id) && StringUtils.isEmty(userCenter)&&StringUtils.isEmty(fileName)){
			path=path+id+"/"+userCenter+"/"+fileName;
			path = new String(path.getBytes("ISO-8859-1"), "utf-8");
			download(path, req, resp);
		}else{
			resp.setContentType("application/json;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(JsonUtil.toJson("error"));
			out.flush();
			out.close();
		}
		
	}

	
	public HttpServletResponse download(String path,HttpServletRequest request, HttpServletResponse response) {
	     StringBuffer sb=new StringBuffer();
		try {
			//File file = new File(request.getSession().getServletContext().getRealPath("/") + "/" + path);
			File file=new File(path);
			String filename = file.getName();
			//System.out.println("绝对路径:"+file.getAbsolutePath());
			sb.append("1绝对路径:"+file.getAbsolutePath()+"\n");
			int length=0;
			InputStream fis = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
			sb.append("2获取io正确\n");
			byte[] buffer = new byte[1024];
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			sb.append("3写io正确\n");
			while((length=fis.read(buffer))!=-1){
				toClient.write(buffer, 0, length);
			}
			sb.append("4写io完成\n");
			fis.close();
			toClient.flush();
			toClient.close();
			sb.append("5全部完成\n");
		} catch (IOException ex) {
			sb.append("获取异常信息:\n"+ex.getMessage()+"\n");
			sb.append("获取本地异常:\n"+ex.getLocalizedMessage());
		}
		IfoContent.sys(sb.toString());
		return response;
	}

	public HttpServletResponse download1(String path,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			// path是指欲下载的文件的路径。 <a href="Download?filename=笔试题">哈哈,测试文件下载</a>
			// File file = new
			// File(request.getSession().getServletContext().getRealPath("/") +
			// "/" + path);
			File file = new File("f:/唐超.doc");
			String filename = file.getName();
			InputStream fis = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.reset();
			response.addHeader("Content-Disposition", "attachment;filename="
					+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response
					.getOutputStream());
			response.setContentType("application/octet-stream");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return response;
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics