Write a JSP upload file with Apache-Commons-Fileupload

zhaozj2021-02-16  113

Two packs and beanutils need to be downloaded, and 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;

// New file object DIR = new java.io.file (path); if (dir == null) {msg = "Error Cause:
Sorry, you can't create a empty directory!"; Return msg;} if (Dir .isfile ()) {msg = "Error reason:
has the same name file Dir.getabsolutePath () " exists. "; return msg;} if (! Dir.exists )) {Boolean result = dir.mkdirs (); if (result == false) {msg = "Error Cause:
Directory " Dir.GetabsolutePath () Creation failure, reasons Unknown! "; Return Msg;} // If you successfully create a directory, no output. // msg = "Successfully created directory: Dir.getabsolutePath () " "; return msg;} else {// msg =" Error Cause:
Directory " Dir.GetabsolutePath () " already exists.";} return msg;} string getcalendar () {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 (); // Settings Allow users to upload file size, unit: byte Fu.setSizeMax (10000000); // maximum size tria Will be stores in Memory? // Set the maximum only allowed to store in memory Data, unit: Byte Fu.setSizTHRESHOLD (4096); // Setting Once the file size exceeds the value of GetSizThreshold (), the data is placed on the hard disk directory fu.setRepositoryPath ("/ tmp"); // Start reading the upload information List FileItems = Fu.Parsequest (Request);

// handle each uploaded file iterator it = fileitems.ITerator (); string updir = "/ dbweb / upics /"; string updir2 = "/ upics /"; string curdate = getCurdate (); string filepath = getServletContext ) .getRealPath (UPDIR2) "/" curdate; // <--- It seems to be a bit problem with getRealPath under Tomcat String OPMSG = MKDIR (filepath); if (opmsg == null) {

While (iter.hasnext ()) {fileItem item = (fileItem item = (fileItem) iter.next (); // ignore other form information if you are not the file field (! item.isformfield ()) {string name = item.getname () Long size = item.getsize (); if ((Name == Null || Name.Equals (")) && size == 0) Continue; // Note that Item.getName () // will return to upload files The client's full path name seems to be a bug. / / To solve this problem, FullFile.getName () is used here. Name = name.replace ('//', '/'); file full full fullfile = new file (name);

File SavedFile = New file (filepath, fullfile.getname ()); item.write (savedfile); msg = ""   fullfile.getname ()   " "; img ="  "  fullfile.getname ()  " "}}} // OPMSG = NULL%>

references:

1. Upload with FileUpload processing file

Author: ◇ Dong-issued time: 2003.07.09 15:52:43, http: //tech.ccidnet.com/pub/disp/Article columnID = 322 & articleID = 53966 & pageNO = 1 made it very clear?

2, Jakarta Commons: Using class and component 1 (2) Author: Vikram Goyal cactus studio Translation, http: //www.uml.org.cn/j2ee/j2eeh2.htm

3. How JSP implements the page redirection, 2002-05-16 · · 包 ·· Yesky, http://www.yesky.com/softchannel/7234237194528521.shtml

4, to determine the existence of a file or folder, author: Luo Kuaitao, 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/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

转载请注明原文地址:https://www.9cbs.com/read-10250.html

New Post(0)