Struts2 the Action upload file get json data

Source: Internet
Author: User

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;
}

}


 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.