萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 609|回复: 0

1-1-16-1-8 JSP数据库插入中文:到底有没有可行的解决办法?

[复制链接]
发表于 2003-2-8 00:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x

  1. 发信人: sleepyworm (睁着眼睡觉的瞌睡虫@_@), 信区: Java      
  2. 标  题: Re: JSP数据库插入中文:到底有没有可行的解决办法?
  3. 发信站: BBS 水木清华站 (Fri Jun 16 21:42:59 2000)

  4. 有关中文问题大家讨论了好久了,
  5. 我自己的问题已经解决了.
  6. 我那天看了看精华版,把各位的方法,汇总了一下.
  7. ---------------------------------------------------------------
  8. java(Jsp)中汉字问题;
  9. 1.
  10. I just know something about the display of Asian Languge.For example, if you have Chinese or Japanese character in yourJava source, when compiling, use "-encoding" option like that:

  11. javac -g -encoding SJIS code_with_Japanese.java

  12. Jdk 1.1 provide the functions to convert local language characterto Uicode. I think it is " getbyte() " ( I'm not so sure ), youcan use it like that:

  13.     getbyte("汉字", Language option);
  14.                         BIG5/GB/SJIS ... the document describing
  15. the language code is released with Jdk1.1, in "Internationalization"part?


  16. 在 JDK Documentation 中查到了此方法,应该是 String.getBytes()。但它只能把 Unicode 转换成相应平台的码制。实际上很多地方要把其它码制转换成 Unicode 码。比如要把用已有的一些中文文本,?Java 直接打开全是乱码。

  17. 2
  18. 从jdbc中取数据,中文字符串,处理方法.下面介绍的方法可以解决从数据库中取数据的中文问题.
  19. 以oracle为例
  20. if(rset.next ())
  21. {
  22.         InputStream inStream=rset.getBinaryStream(i);              
  23.         int avail=inStream.available();               
  24.         byte[] byteTemp=new byte[avail];               
  25.         inStream.read(byteTemp,0,avail);               
  26.         querryData[i-1]=new String(byteTemp,"GBK");   
  27. //注意不要直接使用getString( )
  28. }
  29. 3.
  30. 我在servlet中按前面几位大侠的指导把string用GBK编码成java可认得中文, 但发现把庑﹕tring发送到网页的时候反而成了乱码,用system.out.println 是中文,这怎么解决渴峭页的设置原因吗

  31. 还是我自己来回答吧, 原来java程序中必须用GBK编码才能正常操作, 而在网页上则必须重新用ISO8859_1编码, 所以在发送到网页前需要增加一步转换:
  32. String username = request.getParameter("username");
  33. username = new String(username.getBytes("ISO8859_1"), "GBK");
  34. ......
  35. ......
  36. username = new String(username.getBytes("GBK"), "ISO8859_1");
  37. out.println(username);

  38. 这样就正常了, 不过页面记得用gb2312编码.            

  39. 4
  40. :   这两天在win98下装apache1.3.9加jserv和gnujsp1.0,jdk1.2.2,jsdk2.0
  41. :   发现中文无法正常显示。要么乱码,要么出错。
  42. :   经网友提醒,总结了以下几条方法。
  43. :   1:修改区域设置:在控制面版中选择区域设置,设为英语(美国)?
  44. :      然后重起。一切就都正常。
  45. :   2:在jsp页中加入一条语句:
  46. :      <%@ page contentType="text/html;charset=gb2312" %> ?
  47. :       jsp显示就正常了。
  48. :   3:在编译servlet和jsp时加入代码选项。编译servlet使用
  49. :       javac -encoding iso8859_1 myservlet.java
  50. :      在jsp的zone配置文件中.修改编译参数为:
  51. :       compiler=builtin-javac -encoding ISO8859_1
  52. :     使用这种方法后,不需要作其他的改动就可以正常显示中文了。
  53. :     只是看前面网友的文章在编译servlet时都用GBK码,我试了很多
  54. :     次都不行。而且使用ISO8859_1在原理上难以理解。
  55. :   4:最土的办法,在servlet源程序中加入代码变换语句。如
  56. :      try{
  57. :      out.println(new ( (new String("我爱死你了")).getBytes("GBK"),"ISO8859_1")
  58. :      }
  59. :      catch( UnsupportedEncodingException e)
  60. :      {
  61. :       .......
  62. :       }
  63. :      使用这种方法一定要注意捕获UnsupportedEncodingException这个异常。   

  64. 这个是我的用于数据库取出中文乱码和网络取出中文乱码的处理函数
  65. 其实精华区也有类似的东西。
  66. 入参是有问题的String,出参是解决了的String
  67.         String parseChinese(String in)
  68.         {
  69.                 String s = null;
  70.                 byte temp [];
  71.                 if (in == null)
  72.                 {
  73.                         System.out.println("Warn:Chinese null founded!");
  74.                                 return new String("");
  75.                 }
  76.                 try
  77.                 {
  78.                         temp=in.getBytes("iso-8859-1");
  79.                         s = new String(temp);
  80.                 }                                       
  81.                 {
  82.                         System.out.println("Warn:Chinese null founded!");
  83.                                 return new String("");      
  84.                 }                                       
  85.                 try                                      
  86.                 {
  87.                         temp=in.getBytes("iso-8859-1");      
  88.                         s = new String(temp);               
  89.                 }
  90.                 catch(UnsupportedEncodingException e)
  91.                 {
  92.                         System.out.println (e.toString());
  93.                 }
  94.                 return s;
  95.         }

  96. windows平台上出现中文问题.                                                                 
  97. oracle:
  98. 在application中完全没有问题
  99. 在jsp中 ,
  100. 1).jsp中
  101.   aa.insertString(str1.getBytes("ISO-8859-1")
  102.   aa是一个javabean
  103.   2)aa的实现
  104.    insertString(byte[] b1){
  105.      String s1=new String("GBK");
  106. ...}

  107. 如何在socket中传送汉字

  108. byte[] temp_t;
  109. String temp_p;
  110. temp_p=request.getParameter("message");
  111. temp_t=temp_p.getBytes("ISO8859-1");
  112. String temp=new String(temp_t);

  113. 如何用servlet实现文件的上载?麻烦知道的大虾Re?
  114.   
  115. 给一个例子。(不用多谢,我也是抄来的)
  116.   
  117. public class UploadServlet extends HttpServlet
  118. {
  119.   //default maximum allowable file size is 100k
  120.   static final int MAX_SIZE = 102400;
  121.   //instance variables to store root and success message
  122.   String rootPath, successMessage;
  123.   /**
  124.    * init method is called when servlet is initialized.
  125.    */
  126.   public void init(ServletConfig config) throws ServletException
  127.   {
  128.     super.init(config);
  129.     //get path in which to save file
  130.     rootPath = config.getInitParameter("RootPath");
  131.     if (rootPath == null)
  132.     {
  133.       rootPath = "/";
  134.     }
  135.     /*Get message to show when upload is complete. Used only if
  136.       a success redirect page is not supplied.*/
  137.     successMessage = config.getInitParameter("SuccessMessage");
  138.     if (successMessage == null)
  139.     {
  140.       successMessage = "File upload complete!";
  141.     }
  142.   }
  143.   /**
  144.    * doPost reads the uploaded data from the request and writes
  145.    * it to a file.
  146.    */
  147.   public void doPost(HttpServletRequest request,
  148.     HttpServletResponse response)
  149.   {
  150.     ServletOutputStream out=null;
  151.     DataInputStream in=null;
  152.     FileOutputStream fileOut=null;
  153.     try
  154.     {
  155.       /*set content type of response and get handle to output
  156.         stream in case we are unable to redirect client*/
  157.       response.setContentType("text/plain");
  158.       out = response.getOutputStream();
  159.     }
  160.     catch (IOException e)
  161.     {
  162.       //print error message to standard out
  163.       System.out.println("Error getting output stream.");
  164.       System.out.println("Error description: " + e);
  165.       return;
  166.     }
  167.     try
  168.     {
  169.       //get content type of client request
  170.       String contentType = request.getContentType();
  171.       //make sure content type is multipart/form-data
  172.       if(contentType != null && contentType.indexOf(
  173.         "multipart/form-data") != -1)
  174.       {
  175.         //open input stream from client to capture upload file
  176.         in = new DataInputStream(request.getInputStream());
  177.         //get length of content data
  178.         int formDataLength = request.getContentLength();
  179.         //allocate a byte array to store content data
  180.         byte dataBytes[] = new byte[formDataLength];
  181.         //read file into byte array
  182.         int bytesRead = 0;
  183.         int totalBytesRead = 0;
  184.         int sizeCheck = 0;
  185.         while (totalBytesRead < formDataLength)
  186.         {
  187.           //check for maximum file size violation
  188.           sizeCheck = totalBytesRead + in.available();
  189.           if (sizeCheck > MAX_SIZE)
  190.           {
  191.             out.println("Sorry, file is too large to upload.");
  192.             return;
  193.           }
  194.           bytesRead = in.read(dataBytes, totalBytesRead,
  195.             formDataLength);
  196.           totalBytesRead += bytesRead;
  197.         }
  198.         //create string from byte array for easy manipulation
  199.         String file = new String(dataBytes);
  200.         //since byte array is stored in string, release memory
  201.         dataBytes = null;
  202.         /*get boundary value (boundary is a unique string that
  203.           separates content data)*/
  204.         int lastIndex = contentType.lastIndexOf("=");
  205.         String boundary = contentType.substring(lastIndex+1,
  206.           contentType.length());
  207.         //get Directory web variable from request
  208.         String directory="";
  209.         if (file.indexOf("name="Directory"") > 0)
  210.         {
  211.           directory = file.substring(
  212.             file.indexOf("name="Directory""));
  213.           //remove carriage return
  214.           directory = directory.substring(
  215.             directory.indexOf("\n")+1);
  216.           //remove carriage return
  217.           directory = directory.substring(
  218.             directory.indexOf("\n")+1);
  219.           //get Directory
  220.           directory = directory.substring(0,
  221.             directory.indexOf("\n")-1);
  222.           /*make sure user didn't select a directory higher in
  223.             the directory tree*/
  224.           if (directory.indexOf("..") > 0)
  225.           {
  226.             out.println("Security Error: You can't upload " +
  227.               "to a directory higher in the directory tree.");
  228.             return;
  229.           }
  230.         }
  231.         //get SuccessPage web variable from request
  232.         String successPage="";
  233.         if (file.indexOf("name="SuccessPage"") > 0)
  234.         {
  235.           successPage = file.substring(
  236.             file.indexOf("name="SuccessPage""));
  237.           //remove carriage return
  238.           successPage = successPage.substring(
  239.             successPage.indexOf("\n")+1);
  240.           //remove carriage return
  241.           successPage = successPage.substring(
  242.             successPage.indexOf("\n")+1);
  243.           //get success page
  244.           successPage = successPage.substring(0,
  245.             successPage.indexOf("\n")-1);
  246.         }
  247.         //get OverWrite flag web variable from request
  248.         String overWrite;
  249.         if (file.indexOf("name="OverWrite"") > 0)
  250.         {
  251.           overWrite = file.substring(
  252.             file.indexOf("name="OverWrite""));
  253.           //remove carriage return
  254.           overWrite = overWrite.substring(
  255.             overWrite.indexOf("\n")+1);
  256.           //remove carriage return
  257.           overWrite = overWrite.substring(
  258.             overWrite.indexOf("\n")+1);
  259.           //get overwrite flag
  260.           overWrite = overWrite.substring(0,
  261.             overWrite.indexOf("\n")-1);
  262.         }
  263.         else
  264.         {
  265.           overWrite = "false";
  266.         }
  267.         //get OverWritePage web variable from request
  268.         String overWritePage="";
  269.         if (file.indexOf("name="OverWritePage"") > 0)
  270.         {
  271.           overWritePage = file.substring(
  272.             file.indexOf("name="OverWritePage""));
  273.           //remove carriage return
  274.           overWritePage = overWritePage.substring(
  275.             overWritePage.indexOf("\n")+1);
  276.           //remove carriage return
  277.           overWritePage = overWritePage.substring(
  278.             overWritePage.indexOf("\n")+1);
  279.           //get overwrite page
  280.           overWritePage = overWritePage.substring(0,
  281.             overWritePage.indexOf("\n")-1);
  282.         }
  283.         //get filename of upload file
  284.         String saveFile = file.substring(
  285.           file.indexOf("filename="")+10);
  286.         saveFile = saveFile.substring(0,
  287.           saveFile.indexOf("\n"));
  288.         saveFile = saveFile.substring(
  289.           saveFile.lastIndexOf("\")+1,
  290.           saveFile.indexOf("""));
  291.         /*remove boundary markers and other multipart/form-data
  292.           tags from beginning of upload file section*/
  293.         int pos; //position in upload file
  294.         //find position of upload file section of request
  295.         pos = file.indexOf("filename="");
  296.         //find position of content-disposition line
  297.         pos = file.indexOf("\n",pos)+1;
  298.         //find position of content-type line
  299.         pos = file.indexOf("\n",pos)+1;
  300.         //find position of blank line
  301.         pos = file.indexOf("\n",pos)+1;
  302.         /*find the location of the next boundary marker
  303.           (marking the end of the upload file data)*/
  304.         int boundaryLocation = file.indexOf(boundary,pos)-4;
  305.         //upload file lies between pos and boundaryLocation
  306.         file = file.substring(pos,boundaryLocation);
  307.         //build the full path of the upload file
  308.         String fileName = new String(rootPath + directory +
  309.           saveFile);
  310.         //create File object to check for existence of file
  311.         File checkFile = new File(fileName);
  312.         if (checkFile.exists())
  313.         {
  314.           /*file exists, if OverWrite flag is off, give
  315.             message and abort*/
  316.           if (!overWrite.toLowerCase().equals("true"))
  317.           {
  318.             if (overWritePage.equals(""))
  319.             {
  320.               /*OverWrite HTML page URL not received, respond
  321.                 with generic message*/
  322.               out.println("Sorry, file already exists.");
  323.             }
  324.             else
  325.             {
  326.               //redirect client to OverWrite HTML page
  327.               response.sendRedirect(overWritePage);
  328.             }
  329.             return;
  330.           }
  331.         }
  332.         /*create File object to check for existence of
  333.           Directory*/
  334.         File fileDir = new File(rootPath + directory);
  335.         if (!fileDir.exists())
  336.         {
  337.           //Directory doesn't exist, create it
  338.           fileDir.mkdirs();
  339.         }
  340.         //instantiate file output stream
  341.         fileOut = new FileOutputStream(fileName);
  342.         //write the string to the file as a byte array
  343.         fileOut.write(file.getBytes(),0,file.length());
  344.         if (successPage.equals(""))
  345.         {
  346.           /*success HTML page URL not received, respond with
  347.             generic success message*/
  348.           out.println(successMessage);
  349.           out.println("File written to: " + fileName);
  350.         }
  351.         else
  352.         {
  353.           //redirect client to success HTML page
  354.           response.sendRedirect(successPage);
  355.         }
  356.       }
  357.       else //request is not multipart/form-data
  358.       {
  359.         //send error message to client
  360.         out.println("Request not multipart/form-data.");
  361.       }
  362.     }
  363.     catch(Exception e)
  364.     {
  365.       try
  366.       {
  367.         //print error message to standard out
  368.         System.out.println("Error in doPost: " + e);
  369.         //send error message to client
  370.         out.println("An unexpected error has occurred.");
  371.         out.println("Error description: " + e);
  372.       }
  373.       catch (Exception f) {}
  374.     }
  375.     finally
  376.     {
  377.       try
  378.       {
  379.         fileOut.close(); //close file output stream
  380.       }
  381.       catch (Exception f) {}
  382.       try
  383.       {
  384.         in.close(); //close input stream from client
  385.       }
  386.       catch (Exception f) {}
  387.       try
  388.       {
  389.         out.close(); //close output stream to client
  390.       }
  391.       catch (Exception f) {}

  392.     }
  393.   }
  394. }
  395.   
  396.   


  397. ---------------------------------------------------------------
  398. 【 在 fishes (输了你,赢了世界又如何?) 的大作中提到: 】
  399. :                                                 ^^^^^^^^^
  400. :                                         这个是html中输出中文的编码方式吧
  401. :  你在html页面中显示,这个当然是可以的了
  402. : 插入还是乱码的原因可能是数据库本身的编码方式不对
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-6-3 15:00 , Processed in 0.056988 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表