JSP Smartupload upload files garbled resolution of the documentary +uploadbean upload solution

Source: Internet
Author: User
Tags date format continue file upload reference sleep zip linux
js| Solution | Upload Author: fbysss
Statement: This article for fbysss original, if need to quote, please indicate the author and the reference address
Keywords: smartupload,uploadbean,linux, Chinese garbled
First, smartupload upload files garbled resolution of the documentary
Nausea Index: Five Star
Completion level: 80% (file name problem not resolved)
Problem Description: Web applications developed under Windows, using the Smartupload upload file, the demand is very simple, is a form file formdata.jsp, a Get form and upload the file getdata.jsp, debugging, Windows and Linux system are normal, when the system is installed to another server, it appears to get form parameter value is garbled (all is a question mark??? ) problem.
The first thing I felt in my head was: two machines, according to colleagues, are installed on the same CD-ROM Linux, but I think, logically inferred, it must be the liux is not the same, or the relevant services are not the same. How else could it be? However, the task is left to me to solve, and the system will be formally used, if I asked to reinstall Linux, it is almost impossible. Well, I'm not in hell, who's in hell? I'll break it.
In fact, at this time, I still think the problem is not big. Because Java development of things, garbled problem is commonplace, and the solution is that several. No, just a transfer code? At that time the chief asked me the question is not big, I said, should be no big problem.
I began to GetByte () transcoding, with Iso-8859-1 and GB312/GBK "conversion, not fruit, and joined UTF-8, no;"
Also in the <%@ page contenttype= "text/html;charset=gb2312" > Code replacement, no;
Modify the Linux LANG=ZH_CN. GB2312, no;
Modify the Web.xml charset, no;

At this point I have almost checked all the relevant pages and found the problem related to Smartupload. But the solution is very few, and the solution is very not detailed, so on the CSDN post, the results of a half-day no one to pay attention to, top none. Then on the development group asked, we are busy, also have no way to take into account. Continue to debug their own, with System.getproperty ("file.encoding") to get the result is ansi_x3.4-1968, what is this code? Google looked at it, all of the useless information, at this time I can not help but some chills ... NND, the problem is somewhat serious.
The same day to work, I was not what results, has a head, do not want to work overtime, but spend a half a day not to fix such a "small problem", it is a little face, so I assured the boss that tomorrow I will solve.
Back in the house, tea does not think the rice does not smell. QQ to see, with a mm chat a few words, I said today is very tired, she said I can have a good rest tomorrow. Ah? What day is the week tomorrow? I will check, rely on, tomorrow Saturday Ah! I guess God is destined to work overtime on weekends.
Night did not sleep well, up again is a meal check, still have no good method.
The next day, get up at 11 o ' clock, get up and cook a bowl of noodles and start to do it, put those codes in the form of a new String (Str.getbyte ("iso-8859-1"), "GBK") in a number of combinations, no, how to do, See a change Smartupload source solution, I am very do not want to change the source code, because the server is remote, I do not have the root account, modified class can not be restarted.
Progress one: The formdata.jsp <%@ page contenttype= "text/html;charset=gb2312", take off, plus <meta http-equiv= "Content-type" Content= "text/html; charset=gb2312 "", get the page after the transfer code can be normal display Chinese. But, as we all know, the consequences of this is what, formdta.jsp can not display Chinese! This problem is more difficult to do, and with response.setcontenttype incredibly to the page does not have any effect, really weird.
Time a little past, I finally shaken, I began to try to remember the root of the password, tried, I am incredibly right. OK, finally we can do it.
However, Smartupload source code, incredibly is not easy to find, search for half a day did not. Had to decompile it. The result of gel is not the same as what others say, just make a change,
Locate the public void upload () in Smartupload.java, plus a parameter string charsetname,
Find this string s11 = new String (M_binarray, M_startdata, (m_enddata-m_startdata) + 1);
Modify to String S11 = new String (M_binarray, M_startdata, (m_enddata-m_startdata) + 1,charsetname);
Upload after compiling, restart and try again, found a little progress.
Progress two: Some of the parameters can be read correctly!
This is just a form text box can be, but the file name is still garbled, I have been tossing for a long time, really do not want to go to get the source of the Smartupload, although the code is not much, but I do so to make it too time-consuming. (Note: I think the source code should be able to solve the problem, but did not change the file name that part of the need to deal with.) Interested can try it)

At this time naturally thought of the other upload package, search a bit, someone said Javazoom good, make a Down again, demo is very simple,
After the test found, as long as in the receiving page getdata.jsp Transit code can be. transcoding mode, can be a single parameter to turn, can also be used in one sentence: request.setcharacterencoding ("GBK");
Multipartformdatarequest (Request,null,100*1024*1024,multipartformdatarequest.cosparser, "GBK");
This uploadbean to do very thoughtful, incredibly provide a chineseupload.jsp example, in fact, the above two words are different.
When I was done, it was almost 2 o'clock in the night. Want to relax, sleep first.
Sunday, continue to work, found a new problem, Uploadbean incredibly can't modify uploaded file name! How can this be done? There is a workaround, each file to create a directory, and then put in to prevent duplicate names, but this is a bit disgusting, and directly into the file name or garbled, this can be how the whole ah???
Fortunately, I once again found the superiority of Uploadbean, I found a post, said UploadBean1.5 with Filemover can be renamed, and then the example of 1.5 and Filemover,filemover is also very detailed and simple. It's so sweet.
Modify, upload reboot, everything ok!
Later, the system found several related bugs, bang, all resolved.

This "small problem", incredibly took me a full two days time, I am annoyed! At the same time, also think of the same pain with me brothers, I can alleviate your pain, I have not been in vain for two days! My slogan now is: "Give up smartupload, full use of Uploadbean". I also wrote a demo to help the miserable brothers solve the problem quickly.
Below, I put the demo I wrote and the necessary information links are posted here, we have any questions can be here to communicate with me.

Second, Uploadbean upload solution
Satisfaction Index: five-star
Completion degree: 100%
Demo:
1. Form page sssformdata.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>
&LT;TITLE&GT;FBYSSS Uploadbean Sample </title>
<!--meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 "-->
<!--meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "-->
<form name= "Form1" method= "POST" action= "sssupload.jsp" enctype= "Multipart/form-data" >
<input name= "title" type= "text" value= "Chinese Characters" >
&LT;TD class= "bodystyle" > Accessories </td>
&LT;TD class= "Bodystyle" > <input name= "Attach" type= "FILE" id= "Attach" size= "M" > </td>
<input name= "OK" type= "submit" value= "submitted" >
</form>
2. Read form page sssgetdata.jsp
<!--
//==========================================================================
File: Uploadbean Upload Instance
Features: Solve Chinese garbled, complete file upload, and provide upload name solution
Author: fbysss
Msn:jameslastchina@hotmail.com
//==========================================================================
-->
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page language= "java" import= "com.jspsmart.upload.*"%>
<%@ page import= "Java.text.SimpleDateFormat"%>
<%@ page import= "Java.io.File"%>
<%@ page import= "java.util.*"%>
<%@ page import= "javazoom.upload.*"%>
<%@ page import= "uploadutilities. FileMover "%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<%
Request.setcharacterencoding ("GBK"),//Set the encoding format, you do not have to turn the code.
FileMover FileMover = new FileMover ()//You can also use the form of Jsp:usebean in your own instance.
Uploadbean Upbean = new Uploadbean ();
Multipartformdatarequest mrequest = null;
Hashtable files = null;

if (Multipartformdatarequest.ismultipartformdata (Request))
{

Mrequest = new Multipartformdatarequest (Request,null,100*1024*1024,multipartformdatarequest.cosparser, "GBK"); Note that the encoding parameters are also set here
String sTt0 = Mrequest.getparameter ("title");
Out.println ("<br>title0" is: "+stt0+");
String sTt1 = new String (Stt0.getbytes ("iso-8859-1"), "GBK");
Out.println ("<br>title1" is: "+stt1+");
This is used to test whether the title argument is correct. When debugging, add a sentence of if (true) return;
Files = Mrequest.getfiles ();
}


Get the file name before the modification
String soldfilename =mrequest.getparameter ("Oldfilename");
Out.println ("Soldfilename:" +soldfilename);
String Swebrootpath = Request.getrealpath ("/");//Get the root of your Web application.
String spath=swebrootpath+ "Attach";
int ifilecount = 0;
String sserverfilename= "";
String slocalfilename = "";
File acquisition
if ((Files!= null) | | (!files.isempty ())) {

Ifilecount = Files.size ();
UploadFile file = (uploadfile) files.get ("Attach");
Slocalfilename=file.getfilename ();
Out.println ("Slocalfilename:" +slocalfilename);
int ii= slocalfilename.indexof ("."); Suffix of filename
String sExt = slocalfilename.substring (Ii,slocalfilename.length ());
Get a file name that doesn't duplicate
Java.util.Date dt = new Java.util.Date (System.currenttimemillis ());
SimpleDateFormat FMT = new SimpleDateFormat ("Yyyymmddhhmmsssss");
Sserverfilename= fmt.format (DT);
Sserverfilename =sserverfilename + sExt;
If the directory does not exist, create a new
File dir =new file (spath);
if (!dir.exists ()) {
Dir.mkdirs ();
}
Upbean.setfolderstore (spath);/Set the directory to upload
Upbean.adduploadlistener (FileMover)//Increase Filmover Monitor
Filemover.setnewfilename (Sserverfilename)//Set the file name on the server
Upbean.store (Mrequest, "attach");/upload
Out.println ("File path is" +spath+ "/" +sserverfilename);
}

%>

Demo Note: Cos.jar,uploadbean.jar,filemover.jar These packages must have.
Required Download Address:
Uploadbean1.5:http://wcarchive.cdrom.com/pub/simtelnet/winnt/java/uploadbean1_5.zip
Filemover1.3:http://www.javazoom.net/jzservlets/uploadbean/addons/filemover1.3.zip
Reference Download Address:
chinesupload Example: Http://www.javazoom.net/jzservlets/uploadbean/addons/ChineseUpload.zip




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.