Struts2上传下载
JSP页面
<body>
<s:fielderror></s:fielderror>
<s:form action="uploadUploadAction" enctype="multipart/form-data"
theme="易做图">
用户名:<s:textfield name="userName" />
<br />
密码: <s:textfield name="userPwd" />
<br />
<input type="file" name="file" />
<br />
<s:submit value="提交"></s:submit>
</s:form>
<br />
下载<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());
// 截取的文件扩展名
String fileExtenName = fileFileName
.substring(fileFileName.indexOf('.'));
// System.out.println("截取的文件扩展名"+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 {
//下面以中文名文件实例
//此处文件名称由用户输入,此处也是动态传参的过程
private String picName = "开始.gif";
public InputStream getDownLoad() throws UnsupportedEncodingException {
//此处做一个中间变量,当重新编码后就无法识别中文名了
String rourseName=picName;
//将源文件的中文名重新编码,目的值让Struts的配置文件中能识别到,呈现给用户看
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.execute();
}
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 shortDate(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.DA
补充:Web开发 , Jsp ,