Main jar Package used: Commons-io-1.3.2.jar package; Commons-fileupload-1.2.1.jar package; Commons-lang-2.3.jar, in the use of components to implement file upload should pay attention to the consistency of the front and back, note that in the background definition file file object name to the JSP page with the file tag name name, the foreground form The Label property must be set to Enctype= "Multipart/form-data", and the transfer method should be the post mode. Otherwise, the background does not think that the file type is submitted, but is considered a normal form submission.
No more nonsense. Main implementation code: foreground JSP Code:
<form id= "file_upload_id" name= "File_upload_name" action= "${ctx}/fileprocess/uploadtablefile" method= "POST" Enctype= "Multipart/form-data" >
<table width= "100%" border= "0" align= "center" cellpadding= "0" cellspacing= "0" class= "table_input" >
<tr>
<TD class= "Right" > select Local file: </td><td colspan= "5" >
<input type= "File" size= "id=" "File" name= "file"/>
<input type= "hidden" name= "TableID" value= "<%=ids%>"/>
<input type= "hidden" name= "Tumid" value= "${param.dum}"/>
</td></tr>
</table>
<div class= "clear_10px" ></div>
<table width= "100%" border= "0" align= "center" cellpadding= "0" cellspacing= "0" class= "Table_button" >
<TR><TD align= "left" class= "Btright" ><input type= "submit" value= "Upload" class= "BUTTON_TJ" onclick= " GetInfo () "/></td>
</tr>
</table>
</form>
Background Java code:
Import Java.io.File;
Import Java.io.InputStream;
Import java.io.UnsupportedEncodingException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.commons.io.FilenameUtils;
Import Org.apache.struts2.ServletActionContext;
public class Fileprocessaction extends Baseaction {
Private Filedatumservice Filedatumservice;
Private Attachmentservice Attachmentservice;
Private Accountservice Accountservice;
Private Systemservice Sysservice;
Private Tdatumdetail detail;
Private Tdatum datum;
Private Tablemanage TM;
public Integer ID;
private file file;
Private String Filecontenttype;
Private String Filefilename;
Private String Savepath;
Private String DownloadFileName;
Public String Uploadtablefile () {
Response.setcontenttype ("Text/xml;charset=utf-8");
Boolean ismultipart = servletfileupload.ismultipartcontent (request);
String fileName = "";
try {
if (Ismultipart) {
FileName =getfilefilename ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
String Attacode = Sdf.format (New Date ());
String suffix = filenameutils.getextension (fileName);
String newName = Attacode.concat ("."). concat (suffix);
String BasePath = Request.getrealpath ("/");
System.out.println ("DDD:" +getsavepath ());
Save File
Fileutils.copyfile (GetFile (), New File (Basepath+getsavepath () + "/" +newname));
Save Database
Attachment atta = new Attachment ();
Atta.setattaname (FileName);
Atta.setcurrentname (NewName);
Atta.setuploadtime (New Date ());
Atta.seturl ("/upload/" + newName);
int id=integer.valueof (Request.getparameter ("TableID"));
Tablemanage tabma=new tablemanage ();
Tabma.settablefile (FileName);
Accountservice.tableupdatesingle (ID,TABMA);
Id=attachmentservice.addfileattachment (Atta);
Session.put ("Fileid", id);
Session.put ("Uploadfilename", newName);
return SUCCESS;
}
} catch (Exception e) {
E.printstacktrace ();
}
return "Failure";
}
Public Attachmentservice Getattachmentservice () {
return attachmentservice;
}
public void Setattachmentservice (Attachmentservice attachmentservice) {
This.attachmentservice = Attachmentservice;
}
Public File GetFile () {
return file;
}
public void Setfile (file file) {
This.file = file;
}
Public Tdatumdetail Getdetail () {
return detail;
}
public void Setdetail (Tdatumdetail detail) {
This.detail = detail;
}
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String Getfilecontenttype () {
return filecontenttype;
}
public void Setfilecontenttype (String filecontenttype) {
This.filecontenttype = Filecontenttype;
}
Public String Getfilefilename () {
return filefilename;
}
public void Setfilefilename (String filefilename) {
This.filefilename = Filefilename;
}
Public String Getsavepath () {
return savepath;
}
public void Setsavepath (String savepath) {
This.savepath = Savepath;
}
public void Setdownloadfilename (String downloadfilename) {
This.downloadfilename = DownloadFileName;
}
Public String Getdownloadfilename () {
return downloadfilename;
}
Public Filedatumservice Getfiledatumservice () {
return filedatumservice;
}
public void Setfiledatumservice (Filedatumservice filedatumservice) {
This.filedatumservice = Filedatumservice;
}
Public Accountservice Getaccountservice () {
return accountservice;
}
public void Setaccountservice (Accountservice accountservice) {
This.accountservice = Accountservice;
}
Public Systemservice Getsysservice () {
return sysservice;
}
public void Setsysservice (Systemservice sysservice) {
This.sysservice = Sysservice;
}
Public Tdatum getdatum () {
Return datum;
}
public void Setdatum (Tdatum datum) {
This.datum = Datum;
}
}
You can configure the path of the file to be uploaded in the action inside the Truts.xml
<action name= "Uploadtablefile" class= "Com.sunway.storage.action.FileProcessAction"
method= "Uploadtablefile" >
<param name= "Savepath" >/upload</param>
</action>
File uploads using components such as Commons-fileupload-1.2.1.jar