日期:2014-05-17  浏览次数:20839 次

jsp中文路径问题
路径已经经过utf-8编码,但图片显示不了还是 src里都是经过utf-8编码后的%E5.....jpg

Java code

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!--<jsp:useBean id="smartupload" class="com.jspsmart.upload.SmartUpload"/>-->
 <%@page import="com.jspsmart.upload.SmartUpload"%>
 <%@page import="java.util.*,java.io.*,java.sql.*"%>
 <%@ page import="com.ucap.system.db.*"%>
 
<%
//request.setCharacterEncoding("UTF-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'smartupload04.jsp' starting page</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
  
  <body>
    <%
    
 // request.setCharacterEncoding("utf-8") ;
     //response.setContentType(
  smartupload.initialize(pageContext) ; // 初始化上传
  smartupload.upload() ;     // 准备上传
  String name = smartupload.getRequest().getParameter("uname") ;
   System.out.println(smartupload.getRequest().getParameter("pic"));
  //name=new String(name.getBytes("gbk"),"utf-8");
  //ISO-8859-1
  //request.getParameter("uname");
  //
  System.out.println("name:"+name);
  name = name + "." + smartupload.getFiles().getFile(0).getFileExt() ;
  
  System.out.println(name);
  System.out.println("getServletContext:"+this.getServletContext());
  String fileName = this.getServletContext().getRealPath("/") + "upload/" + name ;
  System.out.println("fileName:"+fileName);
  smartupload.getFiles().getFile(0).saveAs(fileName) ;
  name = java.net.URLDecoder.decode(name,"utf-8"); 
 %>
 
 <img src="upload/<%=name%>" width="300" height="200" /><%=name %>
  </body>
</html>




那个在tomcat\conf\server.xml里 加URLEncoder="utf-8",这个方法 确实可以解决,但这样很多页面里已经写好了的转码就得注释,太多了。


只能在当前页面想办法 求大大帮忙

------解决方案--------------------
很多网站都是不让上传中文名称 的图片的,或者上传之后改成英文名称,建议楼主用这个,不要纠结中文路径的问题了。

tomcat又不是中国人写的是不是……
------解决方案--------------------
页头加上
HTML code

 contentType="text/html; charset=UTF-8"

------解决方案--------------------
瞅瞅,.....

一般我都用的是GBK