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");

设置tomcat 的server.xml
<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;
	}

上面是文件下载时的url中路径和文件名中中文乱码的解决方法。
下面介绍下读取文件时文档乱码的解决办法:
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;
	}

在我艰难的解决这些问题后长舒一口气,在这里写出我的艰难历程,希望能给有这方面困扰的同道中人一点帮助!

你可能感兴趣的:(tomcat,.net,xml)