码迷,mamicode.com
首页 > Web开发 > 详细

文件上传下载过程中中文名称问题

时间:2018-07-02 01:28:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:catch   osi   Servle   bis   file path   contain   i++   ring   else   

 

//上传--
	// 1 保存数据到excel,对应的sheet头header,body数据类型list
	//2 上传文件到服务器中
	//下载--
	// 1 获取文件路径
	//2 导出数据到保持的路径(或者是浏览器路径)

	/**
	 * 保存数据到excel
	 * @param listBody
	 * @param sheetName
	 */
	public void saveExcelData(List<DocLibBrowseOrgVo> listBody, String sheetName) {
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet(sheetName);
		XSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("");
		row.createCell(1).setCellValue("");
		row.createCell(2).setCellValue("");

		int i = 0;
		for (DocLibBrowseOrgVo doc : listBody) {
			i++;
			row = sheet.createRow(i);
			row.createCell(0).setCellValue(doc.getDocLibId());
			row.createCell(1).setCellValue(doc.getDocLibName());
			row.createCell(2).setCellValue(doc.getCount());
			row.createCell(4).setCellValue(doc.getStatisticDate());
		}
	}

	/**
	 * 上传文件到服务器指定路径
	 *
	 * @param destPath
	 * @param fileName
	 */
	public void uploadFile(String destPath, String fileName, XSSFWorkbook wb) throws IOException {
		File pathFile = new File(destPath);
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		File destFile = new File(destPath + fileName);
		BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destFile));
		wb.write(buffer);
		buffer.close();
	}

	/**
	 * 下载
	 *
	 * @param srcPath
	 * @param fileName
	 */
	public void downLoadFile(String srcPath, String fileName, HttpServletRequest request, HttpServletResponse response) {
		File file = new File(srcPath + fileName);
		if (file.exists()) {
			response.setContentType("application/force-download");// 设置强制下载不打开
			setFileDownloadHeader(request, response, fileName); //解决中文名称

			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			OutputStream os = null;
			try {
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				os = response.getOutputStream();//到浏览器下载路径

				int i;
				while ((i=bis.read(buffer))!= -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bis != null) {
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 解决中文名称
	 * @param request
	 * @param response
	 * @param fileName
	 */
	public void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName) {
		try {
			//中文文件名支持
			String encodedfileName;
			String agent = request.getHeader("USER-AGENT");
			if (null != agent && agent.contains("MSIE")) {//IE
				encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
			} else if (null != agent && agent.contains("Mozilla")) {
				encodedfileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			} else {
				encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
			}
			response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

  

 

文件上传下载过程中中文名称问题

标签:catch   osi   Servle   bis   file path   contain   i++   ring   else   

原文地址:https://www.cnblogs.com/Andrew520/p/9251881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!