前後臺分離,後端向前端傳文件

 
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
public class DownloadYhsc extends HttpServlet {
 
	/**
	 * @author admin
	 * @version 2.0
	 */
	private static final long serialVersionUID = 8529206527876956262L;
 
	final protected static Logger logger = LoggerFactory
			.getLogger(Download.class);
 
	@Autowired
	private ExportExcelUtil exportExcelUtil;
 
	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@Override
	@SuppressWarnings({ "unchecked" })
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> paramMap = request.getParameterMap();
		if (null == paramMap || paramMap.isEmpty()) {
			return;
		}
 
		String serverPath = new StringBuilder()
				.append(ArteryUtil.getRequest().getSession()
						.getServletContext().getRealPath(File.separator))
				.append("pub\\yhsc").append(File.separator).toString();
 
		String fileName;
		try {
			fileName = URLEncoder.encode("xxx.docx", "UTF-8");
			String header = new StringBuilder().append("attachment;filename=")
					.append(fileName).toString();
			response.setContentType("application/octet-stream");
			response.addHeader("Content-disposition", header);
			response.addHeader("Transfer-Encoding", null);
			response.addHeader("connection", "close");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
		File file = new File(serverPath + "xxx.docx");
		FileInputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(file);
			out = response.getOutputStream();
			IOUtils.copy(in, out);
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error("文件不存在" + e);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("獲取流異常" + e);
		} finally {
			IOUtils.closeQuietly(out);
			IOUtils.closeQuietly(in);
		}
	}
 
	/**
	 * 轉換編碼
	 * 
	 * @param fileName
	 * @return
	 */
	private String getDecodedStr(String str) {
		String decodedStr = null;
		try {
			if (null == str || str.isEmpty()) {
				decodedStr = StringUtils.EMPTY;
			} else {
				decodedStr = URLDecoder.decode(str, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.error("不支持的編碼" + e);
		}
		return decodedStr;
	}
 
	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
 
	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	@Override
	public void init() throws ServletException {
		// Put your code here
	}
 
	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章