JSP smartupload file garbled solution documentary + uploadbean upload Solution

Source: Internet
Author: User

Author: fbysss
Disclaimer: This article is original in fbysss. If you need to reference it, please indicate the author and reference address
Keywords: smartupload, uploadbean, Linux, Chinese garbled characters
I. smartupload file garbled solution documentary
Nausea index: Five Stars
Degree of completion: 80% (the file name issue is not resolved)
Problem description: A web application developed in Windows uses smartupload to upload files. The requirement is very simple. It is a Form file formdata. JSP, a file getdata that obtains and uploads a form. JSP, during debugging, Windows and Linux systems are normal. When the system is installed on another server, garbled values (all question marks?) are returned ???) .
My first thought in my mind is: two machines, according to my colleagues, are installed on Linux on the same CD, but I think, according to logic, it must be that this liux is installed differently, or the related services are different. Otherwise, how can this problem be solved? However, I have handed over the above task to solve it, and the system will be officially used immediately. If I want to reinstall Linux, it is almost impossible. Well, I am not in hell. Who is in hell? I will try it.
In fact, at this time, I still think the problem is not big. Because of Java Development, garbled code is a common problem, and there are several solutions. Isn't it a transcoding task? At that time, my boss asked me a big question. I said there should be no big questions.
I started getbyte () transcoding, with ISO-8859-1 and gb312/GBK "mutual conversion, did not, and add UTF-8, no;
Encode the content in <% @ page contenttype = "text/html; charset = gb2312">;
Modify Linux lang = zh_cn.gb2312. No;
The charset of Web. xml cannot be modified;

At this time, I have searched almost all related webpages and found this problem is related to smartupload. However, there are few people who can solve the problem, and the solution is not detailed. Therefore, I posted a post on csdn, and the results were ignored for a long time. When I asked questions from the Development Group, everyone was too busy to take care of it. Continue debugging on your own, with system. getproperty ("file. encoding") the result is a ANSI_X3.4-1968, what code is this? I checked it on Google and it was all useless information. At this time, I couldn't help but feel a little cold... Nnd, the problem is serious ..
When I got off work that day, I had no results, and I was so dizzy that I didn't want to work overtime, but I had to spend a long time not solving such a "small problem". It was really a bit of shame, so I assure my boss that I will solve it tomorrow.
Go back to the room and enjoy tea. I went to QQ to check out and chatted with a mm. I said it was so tiring today. She said I could have a good rest tomorrow. Ah? What day of the week is tomorrow? I will verify it. It depends on, tomorrow, Saturday! It seems that I am destined to work overtime on weekends.
If you don't have a good night's sleep, it's a good check.
The next day, I got up at. When I got up and made a bowl of noodles, I started to dry it. I used the codes to use new string (Str. getbyte ("iso-8859-1"), "GBK") this form of a number of combinations, not good, how to do, see a change to the smartupload source code solution, I really don't want to change the source code, because the server is remotely located and I don't have a root account. I can't restart after I modify the class.
Step 1: Set formdata. <% @ page contenttype = "text/html; charset = gb2312">, remove, add <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> to get the Chinese characters displayed after page transcoding. However, we all know what the consequence is. formdta. jsp cannot display Chinese characters! This problem is even harder, and the use of response. setcontenttype has no effect on the page.
A little bit later, I finally got shaken. I began to try to remember the root password. After trying, I was right. OK, you can finally make a big difference.
However, the source code of smartupload is not easy to find, and it has not been searched for a long time. I had to decompile it. The decompiled results with gel are not the same as what others have said, so I just changed the results,
Find public void upload () in smartupload. Java and add the string charsetname parameter,
Find the string S11 = new string (m_binarray, m_startdata, (m_enddata-m_startdata) 1 );
Change to string S11 = new string (m_binarray, m_startdata, (m_enddata-m_startdata) 1, charsetname );
Upload after compilation, restart and try again. A little progress is found.
Progress 2: some parameters are correctly read!
This is only the form text box to pass the value, but the file name is still garbled, I have been tossing for a long time, I really do not want to get the source code of the smartupload, although the Code is not much, however, it takes too much time for me to do this. (Note: I think modifying the source code can solve the problem, but it is not completed. The part of the file name needs to be processed. If you are interested, try it)

At this time, I naturally thought of another upload package and searched for it. Some people said that javazoom is good. Let's talk about it. The demo is very simple,
After the test, we found that transcoding can be performed on the getdata. jsp receiving page. Transcoding method, which can be individually transferred by parameters or in one sentence: request. setcharacterencoding ("GBK ").
Multipartformdatarequest (request, null, 100*1024*1024, multipartformdatarequest. cosparser, "GBK ");
This uploadbean is very considerate, and provides a chineseupload. jsp example. In fact, the above two statements are different.
After I finish the modification, it will be two o'clock in the middle of the night. If you want to relax, go to bed first.
On Sunday, I continued to work and found a new problem. uploadbean could not modify the file name for upload! How can this be done? There is a work und: create a directory for each file and put it in to prevent duplicate names, but this is a bit disgusting, and the file name directly in is still garbled. What is the difference ???
Fortunately, I once again discovered the superiority of uploadbean. I found a Post saying that uploadbean1.5 can be renamed with filemover, so I went down to 1.5 and filemover. The example in filemover is also very detailed and simple. Really considerate...
Modify, upload and restart, everything is OK!
Later, I found several bugs related to the system, all of which were fixed.

This "small problem" took me two days to get angry! At the same time, I also think of brothers who are as painful as me. I can alleviate your pain. I have never done anything in the past two days! My current slogan is: "abandon smartupload and fully use uploadbean ". I also wrote a demo to help the painful brothers solve the problem quickly.
Next, I will paste the demo I wrote and the link to the required materials here. If you have any questions, please contact me here.

Ii. uploadbean upload Solution
Satisfaction Index: Five Stars
Degree of completion: 100%
Demo:
1. form page sssformdata. jsp

 

<% @ Page contenttype = "text/html; charset = gb2312" %>
<Head>
<Title> fbysss uploadbean example </title>
<! -- Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1" -->
<! -- Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" -->
</Head>
<Form name = "form1" method = "Post" Action = "sssupload. jsp" enctype = "multipart/form-Data">
<Input name = "title" type = "text" value = "Chinese Text">
<TD class = "bodystyle"> attachment </TD>
<TD class = "bodystyle"> <input name = "Attach" type = "file" id = "Attach" size = "50"> </TD>
<Input name = "OK" type = "Submit" value = "Submit">
</Form>
2. Read the form page sssgetdata. jsp
<! --
// ================================================ ==========================================
// File: uploadbean upload instance
// Function: solves Chinese garbled characters, completes file upload, and provides a RENAME upload Solution
// By 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" %>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<%
Request. setcharacterencoding ("GBK"); // you do not need to transcode each time you set the encoding format.
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"); // you must set the encoding parameters here.
String stt0 = mrequest. getparameter ("title ");
Out. println ("<br> title0 is:" stt0 "<br> ");
String stt1 = new string (stt0.getbytes ("ISO-8859-1"), "GBK ");
Out. println ("<br> title1 is:" stt1 "<br> ");
// Check whether the title parameter is correct. When debugging, add If (true) return.
Files = mrequest. getfiles ();
}

// Get the file name before modification
String soldfilename = mrequest. getparameter ("oldfilename ");
Out. println ("soldfilename:" soldfilename );
String swebrootpath = request. getrealpath ("/"); // obtain the root of your web application.
String Spath = swebrootpath "Attach ";
Int ifilecount = 0;
String sserverfilename = "";
String slocalfilename = "";
// File Retrieval
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 ("."); // get the filename suffix
String sext = slocalfilename. substring (II, slocalfilename. Length ());
// Get non-repeated file names
Java. util. Date dt = new java. util. Date (system. currenttimemillis ());
Simpledateformat FMt = new simpledateformat ("yyyymmddhhmmsssss ");
Sserverfilename = FMT. Format (DT );
Sserverfilename = sserverfilename sext;
// If this directory does not exist, create
File dir = new file (Spath );
If (! Dir. exists ()){
Dir. mkdirs ();
}
Upbean. setfolderstore (Spath); // you can specify the directory to be uploaded.
Upbean. adduploadlistener (filemover); // added the filmover listener.
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, and filemover. jar must be included.
Required:
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
Refer:
Example of chinesupload: 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.