Create a field of type Java.io.File to receive the uploaded file stream, create xxxfilename and xxxcontenttype to read the original uploaded file name and file type, where xxx represents the field name of the Java.io.File type, such as the example is the file stream that receives the upload named the Upload field, so you need to have uploadfilename and /c8> Uploadcontenttype to receive the original file name and file type of the file stream, the sample code is as follows: ??
public class Uploadaction extends Actionsupport {
??
/**
*
*/
Private static final long serialversionuid = 283051583917637792L;
??
Private File upload;
Private String Uploadfilename;
Private String Uploadcontenttype;
Private String Uploadpath;
??
Public Uploadaction () {
Uploadpath = "Upload";
}
??
@Override
Public String Execute () throws Exception {
??
if (upload = = null) {
Addactionerror (" no option to upload files ");
return INPUT;
}
??
String FilePath = Servletactioncontext.getservletcontext (). Getrealpath (Uploadpath);
??
Java.io.File dir = new Java.io.File (FilePath);
if (dir.exists () = = False) {
if (dir.mkdirs () = = False) {
Addactionerror (" failed to create directory, directory path =" + FilePath);
return INPUT;
}
}
??
System.out.println ("Upload FileName =" + Uploadfilename);
System.out.println ("Upload ContentType =" + Uploadcontenttype);
??
FileOutputStream FileOutputStream = new FileOutputStream (FilePath + file.pathseparator + uploadfilename);
FileInputStream FileInputStream = new FileInputStream (upload);
byte[] buffer = new byte[4096];
int len = 0;
do {
Len = fileinputstream.read (buffer, 0, buffer.length);
if (Len > 0) {
Fileoutputstream.write (buffer, 0, Len);
}
} while (len > 0);
??
Addactionmessage (" upload complete, save path =" + FilePath + file.pathseparator + uploadfilename);
??
return SUCCESS;
}
??
Public File Getupload () {
return upload;
}
??
public void Setupload (File upload) {
This.upload = upload;
}
??
Public String Getuploadfilename () {
return uploadfilename;
}
??
public void Setuploadfilename (String uploadfilename) {
This.uploadfilename = Uploadfilename;
}
??
Public String Getuploadcontenttype () {
return uploadcontenttype;
}
??
public void Setuploadcontenttype (String uploadcontenttype) {
This.uploadcontenttype = Uploadcontenttype;
}
}