我在用SmartUpload组件上传文件时,写了个Servlet对其进行了封装.我在Servlet中输出上传文件的一些信息.结果输出的中文是乱码,但是看页面的源码却都是中文,请哪位高手能解决一下.
下面是我的Servlet源码:
package jspsmart_fileload_test;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import com.facet.jspsmart.upload.*;/**
* <p>Title: </p>
* <p>Description: 附件是带有 jspsmart 包,现在那个网站已经不能下载了所以这里给出 </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author [email protected]
* @version 1.0
*/
public class JspSmartUploadSaveServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String SAVE_DIR_PATH = "C:\\UPLOAD\\";//你可以修改这个设置,以保存到另外的目录
//Initialize global variables
public void init() throws ServletException {
}//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("GBK");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>JspSmartUploadSaveServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a " + request.getMethod() +
". This is the reply.</p>");
out.println("</body>");
out.println("</html>");
out.close();
}//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
PrintWriter out = response.getWriter();
request.setCharacterEncoding("GBK");
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("GBK"); 
out.println("<html>");
out.println("<head><title>JspSmartUploadSaveServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
saveFile(request, response, out);
out.println("<p>File save success " + request.getMethod() +
".<a href=\"jspFileLoad.jsp\">Please go on upload the file!</a></p>");
out.println("</body>");
out.println("</html>");
out.close();
}private void saveFile(HttpServletRequest request,
HttpServletResponse response, PrintWriter out) {
//上传的文件个数
int count = 0;
//文件描述
String FileDescription[] = {null, null, null};
SmartUpload mySmartUpload = new SmartUpload();ServletConfig servletConfig = getServletConfig();
try {
//SmartUpload之初始化,一定要初始化才可以使用
mySmartUpload.initialize(servletConfig, request, response);
//进行相应处理
mySmartUpload.upload();
//最大文件//20mb
mySmartUpload.setMaxFileSize(20 * 1024 * 1024);
} catch (Exception e) {
}//判断有沒有文件描述
//有保存这些信息
//沒有丟入空白字串
if (mySmartUpload.getRequest().getParameter("File1") != null) {
FileDescription[0] = mySmartUpload.getRequest().getParameter("File1");
} else {
FileDescription[0] = "";
}
if (mySmartUpload.getRequest().getParameter("File2") != null) {
FileDescription[1] = mySmartUpload.getRequest().getParameter("File2");
} else {
FileDescription[1] = "";
}
if (mySmartUpload.getRequest().getParameter("File3") != null) {
FileDescription[2] = mySmartUpload.getRequest().getParameter("File3");
} else {
FileDescription[2] = "";
}//取得所有文件信息
File myFile;
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
//产生1个File对象
myFile = mySmartUpload.getFiles().getFile(i);//如果文件存在,则把他存入指定位址
if (!myFile.isMissing()) {
try { //save file
myFile.saveAs(SAVE_DIR_PATH + myFile.getFileName(),
mySmartUpload.SAVE_PHYSICAL);
} catch (Exception ex) {
System.out.println(" error---" + ex.getMessage());
}out.println(this.changeChinese2("文件名 :") + this.changeChinese2(myFile.getFileName()) + "<BR>");
out.println("File type :" + myFile.getContentType() + "<BR>");
out.println("File readme :" + FileDescription[i] + "<BR>");
count++;
out.println("<font color = \"red\">You upload " + count + " files :</font><BR> ");}
}}//Clean up resources
public void destroy() {
}//转换成中文
  public String changeChinese2(String s)
  {
    String s1 = null;
    try
    {
      s1 = new String(s.getBytes("GBK"),"ISO-8859-1");
    }
    catch(Exception em)
    {
      System.out.println(em.getMessage());
    }
    return s1;
  }
}

解决方案 »

  1.   

    补充一下,如果把页面的源码另存后再打开,一切正常,但是字体跟Servlet输出的页面字体并不一样.
      

  2.   

    out.println(this.changeChinese2("文件名 :") + this.changeChinese2(myFile.getFileName()) + "<BR>");
    这里把你的转码去掉试试看
    out.println("文件名 :"+ myFile.getFileName() + "<BR>");
      

  3.   

    加上后是&Icirc;&Auml;&frac14;&thorn;&Atilde;&ucirc;
      

  4.   

    <html>
    <head><title>JspSmartUploadSaveServlet</title></head>
    <body bgcolor="#ffffff">
    文件名 :a.java<BR>
    File type : text/x-java<BR>
    <font color = "red">You upload 1 files :</font><BR> 
    文件名 :aaa.rar<BR>
    File type : application/octet-stream<BR>
    <font color = "red">You upload 2 files :</font><BR> 
    文件名 :checkSql.java<BR>
    File type : text/x-java<BR>
    <font color = "red">You upload 3 files :</font><BR> 
    文件名 :crl.rar<BR>
    File type : application/octet-stream<BR>
    <font color = "red">You upload 4 files :</font><BR> 
    <p>File save success POST.<a href="jspFileLoad.jsp">Please go on upload the file!</a></p>
    </body>
    </html>