url 下载 中文乱码 读取文件中文转码
身为初出茅庐的我 乱码一直让我头疼好久
String targetPath = request.getParameter("path"); String targetFileName = request.getParameter("fileName"); 导致\??????\???????????????淶???????5.3.doc (系统找不到指定的路径。) 我也尝试了好多方法String inStr=request.getParameter("work "); String outStr = new String(inStr.getBytes("iso-8859-1"),"UTF-8"); targetPath = java.net.URLDecoder.decode(targetPath , "UTF-8"); <Connector connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>始终是不行,难受死了
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) {response.setContentType("text/html;charset=GB2312");try {request.setCharacterEncoding("GB2312");} catch (UnsupportedEncodingException e) {logger.error("转码出错!", e);}//path为带文件名的路径String targetPath = request.getParameter("path");String targetFileName = request.getParameter("fileName");if (targetPath != null) {try {targetPath = java.net.URLDecoder.decode(targetPath , "GB2312");} catch (UnsupportedEncodingException e) {logger.error("转码出错!", e);}}if (targetFileName != null) {try {targetFileName = java.net.URLDecoder.decode(targetFileName , "GB2312");} catch (UnsupportedEncodingException e) {logger.error("文件名转码出错!", e);}}byte[] buffer = new byte[SIZE_OF_BUFFER];response.setContentType("application/msword");response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-cache");response.setHeader("Pragma", "no-cache");try { //输出时要进行解码response.setHeader("Content-disposition", "attachment; filename="+ java.net.URLEncoder.encode(targetFileName, "UTF-8"));} catch (UnsupportedEncodingException e) {logger.error("文件名转码出错!", e);}BufferedInputStream bis = null;BufferedOutputStream bos = null;try {ServletOutputStream outputStream = response.getOutputStream();bis = new BufferedInputStream(new FileInputStream(targetPath));bos = new BufferedOutputStream(outputStream);int bytesRead;while (-1 != (bytesRead = bis.read(buffer, 0, buffer.length))) {bos.write(buffer, 0, bytesRead);}logger.debug(bos);} catch (FileNotFoundException e) {logger.error("创建输入流时出错!", e);} catch (IOException e) {logger.error("下载时出错!", e);} finally {if (bis != null) {try {bis.close();} catch (IOException e) {logger.error("关闭输入流时出错!", e);}}if (bos != null) {try {bos.close();} catch (IOException e) {logger.error("关闭输出流时出错!", e);}}}return null;}public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) {ModelAndView mav = new ModelAndView();//接收链接传递的路径参数String path = request.getParameter("path");//接收链接传递的行数String line = request.getParameter("line");//接收链接传递的相对路径参数String relativePath = request.getParameter("relativePath");//接收链接传递的文件名String fileName = request.getParameter("fileName");//接收文件类型String fileType = request.getParameter("fileType");//接收字符编码类型String charSet = request.getParameter("charSet");if (!typeMap().containsKey(fileType) || fileType == null) {fileType = "html";}//读取文件中信息StringBuffer contents = new StringBuffer();BufferedReader bufferReader = null;try {File dir = new File(path);FileInputStream fileinput = new FileInputStream(dir);InputStreamReader isrGBK;//根据文件编码格式读取文件isrGBK = new InputStreamReader(fileinput, charSet);bufferReader = new BufferedReader(isrGBK);String strGBK = null;while ((strGBK = bufferReader.readLine()) != null) {//逐行放入bufferReader中contents.append(strGBK).append("\r\n");logger.debug("4444444444" + strGBK);}mav.addObject("fieldset", contents);} catch (Exception e) {logger.error("读取文件错误信息:" , e);} finally {if (bufferReader != null) {try {bufferReader.close();} catch (IOException e) {logger.error("关闭缓冲流的错误:" , e);}}}mav.addObject("line", line);mav.addObject("fileType", fileType);mav.addObject("relativePath", relativePath);mav.addObject("fileName", fileName);mav.setViewName(successView);return mav;}