Java Code:
[Java]
Package com. shop. action;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. http. HttpServletRequest;
Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;
Import com. shop. uitl. FileUitl;
Import com. shop. uitl. ServerUrl;
Public class FileUpload extends ActionSupport {
Private File file; // the uploaded File.
Private String fileFileName; // file name
Private String fileContentType; // file type
Private String path; // return path
Private boolean status; // status
Private String message; // message
Private String url; // The call path of the returned url.
Public String getUrl (){
Return url;
}
Public void setUrl (String url ){
This. url = url;
}
Public void setFile (File file ){
This. file = file;
}
Public void setFileFileName (String fileFileName ){
This. fileFileName = fileFileName;
}
Public String getFileFileName (){
Return fileFileName;
}
Public String getFileContentType (){
Return fileContentType;
}
Public void setFileContentType (String fileContentType ){
This. fileContentType = fileContentType;
}
Public String getPath (){
Return path;
}
Public void setPath (String path ){
This. path = path;
}
Public boolean isStatus (){
Return status;
}
Public void setStatus (boolean status ){
This. status = status;
}
Public String getMessage (){
Return message;
}
Public void setMessage (String message ){
This. message = message;
}
@ Override
Public String execute () throws Exception {
// TODO Auto-generated method stub
HttpServletRequest request = ServletActionContext. getRequest ();
String path = ServerUrl. getServerPath (request, "upload ");
String save = ServerUrl. getDiskPath (request, "upload ");
If (file = null ){
This. setMessage ("the file is empty! ");
Status = false;
Return SUCCESS;
}
Try {
// Create a directory based on the date of the day
Date date = new Date ();
SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd ");
SimpleDateFormat format2 = new SimpleDateFormat ("yyyyMMddhhmmss ");
String strDate = format. format (date );
// Check whether the folder exists
If (! New File (save + strDate). isDirectory ()){
New File (save + strDate). mkdir ();
}
// Rename the file
String name = FileUitl. getExtensionName (fileFileName );
FileFileName = format2.format (date) + name;
// Write to disk
FileInputStream in = new FileInputStream (file );
File outFile = new File (save + strDate + "\" + fileFileName );
FileOutputStream out = new FileOutputStream (outFile );
// File Corruption
Int ch = 0;
While (ch = in. read ())! =-1 ){
Out. write (ch );
}
Out. flush ();
Out. close ();
In. close ();
SetMessage ("the file is uploaded successfully! "); // Response Information
SetPath ("upload/" + strDate + "/" + fileFileName); // relative path
SetStatus (true); // status
SetUrl (ServerUrl. getServerPath (request, "upload/" + strDate + "/" + fileFileName); // absolute path
System. out. println (fileContentType + "received data! ");
Return SUCCESS;
} Catch (Exception e ){
SetStatus (false );
SetMessage ("an exception occurred while uploading. Upload Failed! ");
}
Return SUCCESS;
}
}
Package com. shop. action;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. servlet. http. HttpServletRequest;
Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;
Import com. shop. uitl. FileUitl;
Import com. shop. uitl. ServerUrl;
Public class FileUpload extends ActionSupport {
Private File file; // the uploaded File.
Private String fileFileName; // file name
Private String fileContentType; // file type
Private String path; // return path
Private boolean status; // status
Private String message; // message
Private String url; // The call path of the returned url.
Public String getUrl (){
Return url;
}
Public void setUrl (String url ){
This. url = url;
}
Public void setFile (File file ){
This. file = file;
}
Public void setFileFileName (String fileFileName ){
This. fileFileName = fileFileName;
}
Public String getFileFileName (){
Return fileFileName;
}
Public String getFileContentType (){
Return fileContentType;
}
Public void setFileContentType (String fileContentType ){
This. fileContentType = fileContentType;
}
Public String getPath (){
Return path;
}
Public void setPath (String path ){
This. path = path;
}
Public boolean isStatus (){
Return status;
}
Public void setStatus (boolean status ){
This. status = status;
}
Public String getMessage (){
Return message;
}
Public void setMessage (String message ){
This. message = message;
}
@ Override
Public String execute () throws Exception {
// TODO Auto-generated method stub
HttpServletRequest request = ServletActionContext. getRequest ();
String path = ServerUrl. getServerPath (request, "upload ");
String save = ServerUrl. getDiskPath (request, "upload ");
If (file = null ){
This. setMessage ("the file is empty! ");
Status = false;
Return SUCCESS;
}
Try {
// Create a directory based on the date of the day
Date date = new Date ();
SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd ");
SimpleDateFormat format2 = new SimpleDateFormat ("yyyyMMddhhmmss ");
String strDate = format. format (date );
// Check whether the folder exists
If (! New File (save + strDate). isDirectory ()){
New File (save + strDate). mkdir ();
}
// Rename the file
String name = FileUitl. getExtensionName (fileFileName );
FileFileName = format2.format (date) + name;
// Write to disk
FileInputStream in = new FileInputStream (file );
File outFile = new File (save + strDate + "\" + fileFileName );
FileOutputStream out = new FileOutputStream (outFile );
// File Corruption
Int ch = 0;
While (ch = in. read ())! =-1 ){
Out. write (ch );
}
Out. flush ();
Out. close ();
In. close ();
SetMessage ("the file is uploaded successfully! "); // Response Information
SetPath ("upload/" + strDate + "/" + fileFileName); // relative path
SetStatus (true); // status
SetUrl (ServerUrl. getServerPath (request, "upload/" + strDate + "/" + fileFileName); // absolute path
System. out. println (fileContentType + "received data! ");
Return SUCCESS;
} Catch (Exception e ){
SetStatus (false );
SetMessage ("an exception occurred while uploading. Upload Failed! ");
}
Return SUCCESS;
}
}