Use APACHE-commons-fileupload to write a JSP file to be uploaded

Source: Internet
Author: User
Use APACHE-commons-fileupload to write a JSP file to be uploaded

You need to download the fileupload and beanutils packages. The Code is as follows.

<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. util. *" %>

<% @ Page import = "Java. Io. *" %>

<% @ Page import = "org. Apache. commons. fileupload. *" %>

<%!
String mkdir (string path ){

String MSG = NULL;
Java. Io. File dir;

// Create a file object
Dir = new java. Io. File (PATH );
If (DIR = NULL ){
MSG = "error cause: <br> sorry, you cannot create an empty directory! ";
Return MSG;
}
If (dir. isfile ()){
MSG = "error cause: <br> an existing file with the same name <B>" + dir. getabsolutepath () + "</B> exists. ";
Return MSG;
}
If (! Dir. exists ()){
Boolean result = dir. mkdirs ();
If (result = false ){
MSG = "error cause: <br> directory <B>" + dir. getabsolutepath () + "</B> creation failed. The cause is unknown! ";
Return MSG;
}
// If the directory is successfully created, no output is displayed.
// MSG = "successfully created Directory: <B>" + dir. getabsolutepath () + "</B> ";
Return MSG;
} Else {
// MSG = "error cause: <br> directory <B>" + dir. getabsolutepath () + "</B> already exists. ";
}
Return MSG;
}
String getcurdate (){
Gregoriancalendar gcdate = new gregoriancalendar ();
Int year = gcdate. Get (gregoriancalendar. year );
Int month = gcdate. Get (gregoriancalendar. month );
Int day = gcdate. Get (gregoriancalendar. day_of_month );
Return "" + year + month + Day;
}
%>
<%
String MSG = "";
String IMG = NULL;

Diskfileupload Fu = new diskfileupload ();
// Set the size of the file to be uploaded. Unit: bytes.
Fu. setsizemax (10000000 );
// Maximum size that will be stored in memory?
// Set up to only data that can be stored in the memory, in bytes
Fu. setsizethreshold (4096 );
// Set the data stored in the hard disk directory when the file size exceeds the value of getsizethreshold ()
Fu. setrepositorypath ("/tmp ");
// Start reading upload information

List fileitems = Fu. parserequest (request );

// process each uploaded file in sequence
iterator iter = fileitems. iterator ();
string updir = "/dbweb/upics/";
string updir2 = "/upics/";
string curdate = getcurdate ();
string filepath = getservletcontext (). getrealpath (updir2) + "/" + curdate; // <--- it seems that there is a problem with getrealpath under Tomcat
string opmsg = mkdir (filepath );
If (opmsg = NULL) {

while (ITER. hasnext () {
fileitem item = (fileitem) ITER. next ();
// ignore all other form information that is not a file field
If (! Item. isformfield () {
string name = item. getname ();
long size = item. getsize ();
If (name = NULL | Name. equals ("") & size = 0)
continue;
// pay attention to item. getname ()
// returns the full path name of the uploaded file on the client. This seems to be a bug.
// to solve this problem, fullfile. getname () is used ().
name = Name. Replace ('\', '/');
file fullfile = new file (name);

File savedfile = new file (filepath, fullfile. getname ());
Item. Write (savedfile );
MSG = " ";
IMG = " ";
}
}
} // Opmsg = NULL
%>

References:
1. Use fileupload to process file uploads
Author: ◇ Liu Dong posting time: 2003.07.09 15:52:43, http://tech.ccidnet.com/pub/disp/Article? Columnid = 322 & ArticleID = 53966 & pageno = 1
Speak very clearly
2. Jakarta commons: Skillful classes and components 1 (2) by Vikram Goyal cactus studio, http://www.uml.org.cn/j2ee/j2eeh2.htm

3, JSP how to achieve web page redirection, 2002-05-16 · BaO luyue · yesky, http://www.yesky.com/SoftChannel/72342371945283584/20020424/1608521.shtml

4, judge whether the file or folder exists, Author: Luo will Tao, http://www.fawcette.com/china/XmlFile.aspx? Id = 205

5. Date class in Java. util, by wing, source: everywhere, http://www.linuxaid.com.cn/articles/1/1/119413536.shtml

6. Computing Java date -- learn how to create and use date, Author: Robert Nielsen, http://www.javaresearch.org/article/showarticle.jsp? Column = 1 & Thread = 497

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.