js| uploaded in the use of Java development Enterprise system, especially in connection with the Office-related software development, the file upload is often referred to the requirements of customers. So a good file upload solution can also facilitate the development of this piece.
First of all, the article is for their own record of the development needs after the time, do not rush haha ...
Now in the domestic use of very many generally is two methods to solve the file upload.
Cos.jar + Uploadbean.jar + Filemover.jar
This is very common because of the ease with which we have no need to pay attention to the input and output stream of those files, which frees us from the underlying stream.
Uploadfile,uploadbean,multipartformdatarequest
<%@ page contenttype= "text/html;charset=gb2312"%>
<TITLE>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" >
<TD class= "bodystyle" > Accessories </td>
<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
Refer to an article in Fbysss
2. Org.apache.struts.upload.FormFile
The Org.apache.struts.upload.FormFile class of the Struts1.1. It's very convenient not to write on your own. Also do not have to write a JSP call Jspsmartupload can be done.
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.