The second document upload
File upload is also a very common application in web development, common methods:
One. HTML tag
Usually we can select components in the background, such as Smartupload, etc. In the Struts framework, Struts uses a Common-FileUPload package that can be easily uploaded.
As shown below, the front page:
The Action processed in the background is as follows:
Package maoxiang.examples.Web.Actions;
Import java.io.file;
Import java.io.fileoutputstream;
Import java.io.ioException;
Import Java.io.InputStream;
Import maoxiang.common.web.GenericAction;
Import Maoxiang.examples.Web.Forms.uploadForm;
Import org.apache.struts.upload.formfile;
/ **
* @ Struts.Action path = "/ upload / upload"
Name = "UploadForm"
*
Scope = "request"
INPUT = "/ upload / upload1.jsp"
*
Validate = "True"
*
* /
Public final class uploadAction extends genericction {
protected boolean action () {
IF ("Upload" .Equals (getAction ())) {
LOG.INFO ("I am in Upload");
Try {
UploadForm Upload = (UploadForm) Form;
// Get uploaded files
Formfile file = UPLOAD.GETTHEFILE ();
// Save it to / example / temp
// String contextPath = getServlet (). GetServletContext (). GetRealPath (
//
"/ esamples / upload");
// Write the file into the TEMP file
InputStream Instream = file.getinputStream ();
File Temp = file.createTempfile ("Temp", String.Valueof (System
.currenttimemillis ()));
Log.info ((Temp.getabsolutePath ()));
FileOutputStream outstream = new fileoutputstream (TEMP);
INT BYTESREAD = 0;
Byte [] buffer = new byte [8192];
Long length = 0;
While ((BytesRead = Instream.read (buffer))! = -1) {
Outstream.write (buffer, 0, bytesread);
Length = bytesRead;
}
Instream.close ();
Outstream.close ();
Request.getSession (). SetAttribute ("Para1", Upload.getPara1 ());
Request.getations (). setttribute ("file", temp.getabsolutepath ());
Return True;
} catch (ioexception e) {
LOG.Error ("" error occurred while uploading file " E.getMessage ());
Return False;
}
}
Return False;
}
}
It seems that he and the general Action have no difference, it is very convenient.
Second. Special upload tool
Since the HTML tag is used, many features cannot be done. If the file is restricted before the upload, only the picture can be uploaded, the size can only be less than 2m. The HTML tag can only be uploaded to the background before it can be judged and prompt, and it is obviously not high.
The following is the upload tool made on SWT FileViewer:
Of course, there are some inconveniences in this way:
1. Require each user to download this program, how to install
2. If the program is updated, how to synchronize
In a Java environment, these two issues can be solved by Java Web Start technology.
Place a startup program on the page (in accordance with JNLP protocol) start up.
JNLP is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
information>
security>
resources>
resources>
jnlp> About how to start SWT using Java Web Start, you can check the related articles on IBM-900.
Summary
These two uploading methods have a disadvantage. It is worth mentioning that the latter, using Java Web Start technology to realize the local launch of the program, especially for SWT applications, more than applets.