Struts2 upload/download

Source: Internet
Author: User
Tags idate

JSP page
<Body>
<S: fielderror> </s: fielderror>
<S: form action = "uploadUploadAction" enctype = "multipart/form-data"
Theme = "simple">
UserName: <s: textfield name = "userName"/>
<Br/>
Password: <s: textfield name = "userPwd"/>
<Br/>
<Input type = "file" name = "file"/>
<Br/>
<S: submit value = "submit"> </s: submit>
</S: form>
<Br/>
Download <a href = "DownLoadAction"> .gif </a>
</Body>
UploadAction
 


Package com. hyl. action;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import org. apache. struts2.ServletActionContext;
Import com. hyl. util. DateUtil;
Import com. opensymphony. xwork2.ActionSupport;

Public class UploadAction extends ActionSupport {
Private File file;
Private String fileFileName;
Private String fileContentType;
Private String userName;
Private String userPwd;

Public String upload () throws IOException {
String path = ServletActionContext. getRequest (). getRealPath ("/upload ");
// System. out. println (path );

InputStream is = new FileInputStream (file );
String date = DateUtil. mailDate (new java. util. Date ());
// Intercept the file extension
String fileExtenName = fileFileName
. Substring (fileFileName. indexOf ('.'));
// System. out. println ("intercepted file extension" + fileName );

File serverFile = new File (path, date + fileExtenName );

OutputStream OS = new FileOutputStream (serverFile );

Byte [] B = new byte [1024];

Int length = 0;

While (length = is. read (B)> 0 ){
OS. write (B );

}
OS. close ();
Is. close ();
Return SUCCESS;
}

Public File getFile (){
Return file;
}

Public void setFile (File file ){
This. file = file;
}

Public String getFileFileName (){
Return fileFileName;
}

Public void setFileFileName (String fileFileName ){
This. fileFileName = fileFileName;
}

Public String getFileContentType (){
Return fileContentType;
}

Public void setFileContentType (String fileContentType ){
This. fileContentType = fileContentType;
}

Public String getUserName (){
Return userName;
}

Public void setUserName (String userName ){
This. userName = userName;
}

Public String getUserPwd (){
Return userPwd;
}

Public void setUserPwd (String userPwd ){
This. userPwd = userPwd;
}

}
DownLoadAction

 

Package com. hyl. action;

Import java. io. InputStream;
Import java. io. UnsupportedEncodingException;
Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;

Public class DownLoadAction extends ActionSupport {
// The following example uses a Chinese file name
// Here, the file name is input by the user. This is also the process of dynamically passing parameters.
Private String picName = "..gif ";

Public InputStream getDownLoad () throws UnsupportedEncodingException {
// Make an intermediate variable here. After re-encoding, the Chinese name cannot be recognized.
String rourseName = picName;
// Recode the Chinese name of the source file. The target value can be identified in the Struts configuration file and displayed to the user.
PicName = new String (picName. getBytes (), "iso-8859-1 ");
 
System. out. println ("/upload/" + rourseName );
 
Return ServletActionContext. getServletContext (). getResourceAsStream (
"/Upload/" + rourseName );
 
}

Public String execute () throws Exception {

Return super.exe cute ();
}

Public String getPicName (){
Return picName;
}

Public void setPicName (String picName ){
This. picName = picName;
}

}
DateUtil

 

Package com. hyl. util;

Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
Import java. util. GregorianCalendar;

Public class DateUtil {

Public static String dateTimeChange (Date source ){
SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String changeTime = format. format (source );
Return changeTime;
}

Public static String updated Date (Date aDate ){
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy ");
Return formatter. format (aDate );
}

Public static String nowDate (){
String iDate = "";
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");
String str = formatter. format (new Date ());
String [] date = str. split ("-");
If (date. length> = 3 ){
IDate = date [0] + "/" + date [1] + "/" + date [2] + "";
} Else {
IDate = str;
}
Return iDate;
}

Public static String mailDate (Date aDate ){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS ");
Return formatter. format (aDate );
}

Public static String dateParser (Date aDate ){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");
Return formatter. format (aDate );
}

Public static Date parser (String strDate ){
;
StrDate = strDate. replace ("/","-");
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Try {
Return sdf. parse (strDate );
} Catch (Exception e ){
Return null;
}
}

Public static Date parser (String strDate, String formatter ){
SimpleDateFormat sdf = new SimpleDateFormat (formatter );
Try {
Return sdf. parse (strDate );
} Catch (Exception e ){
Return null;
}
}

Public static String parser (Date date, String formatter ){
SimpleDateFormat sdf = new SimpleDateFormat (formatter );
Try {
Return sdf. format (date );
} Catch (Exception e ){
Return null;
}
}

Public static Date addMonth (Date myDate, int amount ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (myDate );
Boolean isEndDayOfMonth_old = cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH) = cal
. Get (GregorianCalendar. DAY_OF_MONTH );
Cal. add (GregorianCalendar. MONTH, amount );
Boolean isEndDayOfMonth_new = cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH) = cal
. Get (GregorianCalendar. DAY_OF_MONTH );
If (isEndDayOfMonth_old &&! IsEndDayOfMonth_new ){
Cal. set (GregorianCalendar. DATE, cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH ));
}
Return cal. getTime ();
}

Public static Date addDay (Date myDate, int amount ){
Calendar cal = Calendar. getInstance ();
Cal. setTime (myDate );
Cal. add (Calendar. DAY_OF_MONTH, amount );
Return cal. getTime ();
}

Public static Date addMinute (Date myDate, int amount ){
Calendar cal = Calendar. getInstance ();
Cal. setTime (myDate );
Int minute = 0;
Amount =-(amount );

If (amount> 60 ){

Int hour = (int) amount/60;

If (hour * 60> amount ){
Minute = hour * 60-amount;

Cal. add (Calendar. HOUR_OF_DAY,-hour );
Cal. add (Calendar. MINUTE, minute );

} Else if (hour * 60 <amount ){

Minute = amount-hour * 60;
Cal. add (Calendar. HOUR_OF_DAY,-hour );
Cal. add (Calendar. MINUTE,-minute );

} Else {
Cal. add (Calendar. HOUR_OF_DAY,-hour );
}

} Else {

Cal. add (Calendar. MINUTE,-amount );
}
Return cal. getTime ();
}

Public static Date addYear (Date myDate, int amount ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (myDate );
Boolean isEndDayOfMonth_old = cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH) = cal
. Get (GregorianCalendar. DAY_OF_MONTH );
Cal. add (GregorianCalendar. YEAR, amount );
Boolean isEndDayOfMonth_new = cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH) = cal
. Get (GregorianCalendar. DAY_OF_MONTH );
If (isEndDayOfMonth_old &&! IsEndDayOfMonth_new ){
Cal. set (GregorianCalendar. DATE, cal
. GetActualMaximum (GregorianCalendar. DAY_OF_MONTH ));
}
Return cal. getTime ();
}

Public static int getWeekDay (Date myDate ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (myDate );
Return cal. get (GregorianCalendar. DAY_OF_WEEK );
}

Public static int getConvertWeekDay (Date myDate ){
Int day = getWeekDay (myDate );
Int result = day-1;
If (result = 0)
Result = 7;
Return result;
}

Public static int getTimeFromDate (Date myDate ){
SimpleDateFormat sdf = new SimpleDateFormat ("hhmmss ");
Int result = Integer. parseInt (sdf. format (myDate ));
Return result;
}

Public static long getDaysBetweenDate (Date startDate, Date endDate ){
Calendar cal = Calendar. getInstance ();
Cal. setTime (startDate );
Cal. set (Calendar. HOUR, 0 );
Cal. set (Calendar. MINUTE, 0 );
Cal. set (Calendar. SECOND, 0 );
Cal. set (Calendar. MILLISECOND, 0 );
StartDate = cal. getTime ();
Cal. setTime (endDate );
Cal. set (Calendar. HOUR, 0 );
Cal. set (Calendar. MINUTE, 0 );
Cal. set (Calendar. SECOND, 0 );
Cal. set (Calendar. MILLISECOND, 0 );
Return (cal. getTime (). getTime ()-startDate. getTime ()/86400000;

}

Public static String strDateTime (String str ){
String idate = "";
If (str! = Null ){
String [] date = str. split ("-");
If (date. length> = 3 ){
Idate = date [0] + "." + date [1] + "." + date [2];
} Else {
Idate = str;
}
}
Return idate;
}

Public static String strDotDateTime (String str ){
String idate = "";
If (str! = Null ){
String data0 = null;
String [] date = str. split ("-");
If (date. length> = 3 ){
If (date [0]! = Null ){
Data0 = date [0]. substring (2, 4 );
}
Idate = data0 + "." + date [1] + "." + date [2];
} Else {
Idate = str;
}
}
Return idate;
}

Public static String bakDateTime (String str ){
String idate = "";
If (str! = Null ){
Int l1 = str. indexOf (".");
String d1 = str. substring (0, l1 );
String s1 = str. substring (l1 + 1 );
Int l2 = s1.indexOf (".");
String d2 = s1.substring (0, l2 );
String d3 = s1.substring (l2 + 1 );
Idate = d1 + "-" + d2 + "-" + d3;
}
Return idate;
}

Public static String strShortDateTime (String str ){
String idate = "";
If (str! = Null ){
String [] date = str. split ("-");
If (date. length> = 3 ){
Idate = date [0] + "." + date [1] + "." + date [2];
} Else {
Idate = str;
}
If (idate! = Null & idate. length ()> 9 ){
Idate = idate. substring (0, 10 );

}
}
Return idate;
}

Public static int getBetweenDayNumber (String dateA, String dateB ){
Long dayNumber = 0;
Long DAY = 24L * 60L * 60L * 1000L;
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd ");
Try {
Java. util. Date d1 = df. parse (dateA );
Java. util. Date d2 = df. parse (dateB );
DayNumber = (d2.getTime ()-d1.getTime ()/DAY;
} Catch (Exception e ){
E. printStackTrace ();
}
Return (int) dayNumber;
}

Public static void main (String [] args ){
System. out. println (nowDate ());
}
}
MessageFile. properties

Struts. messages. error. file. too. large = \ u6587 \ u4EF6 \ u8FC7 \ u5927
Struts. messages. error. content. type. not. allowed = \ u6587 \ u4EF6 \ u7C7B \ u578B \ u4E0D \ u4E00 \ u81F4

Struts. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<Struts>
<! -- Specify the file name of messageFile. properties in the value of the message resource file that loads Struts. Do not write the extension name -->
<Constant name = "struts. custom. i18n. resources" value = "messageFile"> </constant>
<Package name = "hyl" extends = "struts-default">
<! -- Upload Action -->
<Action name = "* UploadAction" class = "com. hyl. action. UploadAction"
Method = "{1}">
<Result name = "success">/OK. jsp </result>
<Result name = "input">/index. jsp </result>
<Interceptor-ref name = "fileUpload">
<Param name = "allowedTypes"> image/jpeg </param>
<Param name = "maximumSize"> 102400 </param>
</Interceptor-ref>
<! -- The default interceptor must be placed under the custom interceptor; otherwise, the custom interceptor cannot be called. -->
<Interceptor-ref name = "defaultStack"> </interceptor-ref>
</Action>
<! -- Downloaded Action -->
<Action name = "DownLoadAction" class = "com. hyl. action. DownLoadAction">
<Result name = "success" type = "stream">
<! -- The specified return type is displayed here. For example, the type that can be recognized by the browser is directly displayed,
If it is another type, it will appear as a download,
If this label is removed, the default text/plain
-->
<Param name = "contentType"> image/jpeg </param>
<! -- The method here is the getDownLoad method in Action, -->
<Param name = "inputName"> downLoad </param>
<Param name = "contentDisposition"> filename = "$ {picName}" </param>
<Param name = "bufferSize"> 1024 </param>
</Result>
</Action>

</Package>

</Struts>

 


Author: hyljava

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.