package com.struts2.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class fileUploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 16*1024;
/*
* 注意:
* uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名、上传文件的文件类型。这两个属性,体现了Struts2设计的灵巧、简化之处,Action类直接通过File类型属性直接封装了上传文件的文件内容,但这个File属性无法获取上传文件的文件名和文件类型,所以Struts2直接将文件域中包含的上传文件名和文件类型的信息封装到uploadFileName和uploadContentType属性中。可以认为:如果表单中包含一个name属性为xxx的文件域,则对应Action需要使用三个属性来封装该文件域的信息:
* 类型为File的xxx属性封装了该文件域对应的文件内容。
* 类型为String的xxxFileName属性封装了该文件域对应的文件的文件名。
* 类型为String的xxxContentType属性封装了该文件域对应的文件的文件名。
*
*/
private String mfileContentType;//封装上传文件类型的属性,必须是文件名+ContentType
private File mfile;
private String mfileFileName;//封装上传文件名的属性,必须是文件名+FileName
private String imageFileName;//上传后保存的文件名
private String caption;
private String savePath;//
private String allowedTypes;
public String filterType(String[] types)
{
String fileType = getMfileContentType();
for (String type:types)
{
if (type.equals(fileType))
{
return null;
}
}
return INPUT;
}
private static void copy(File src,File dst)
{
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer= new byte[BUFFER_SIZE];
try {
while (in.read(buffer)>0)
{
out.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (in==null)
{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out ==null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//取文件的扩展名
private static String getExtention(String fileName)
{
int pos = fileName.lastIndexOf('.');
return fileName.substring(pos);
}
public String execute()
{
if (mfileFileName==null || mfileFileName.equals("")){
return INPUT;
}
else
{
imageFileName = new Date().getTime() + getExtention(getMfileFileName());
//取文件保存的真实路径
File imageFile = new File(ServletActionContext.getServletContext().getRealPath(savePath) + "/" + imageFileName);
copy(mfile, imageFile);
return SUCCESS;
}
//return INPUT;
}
public String getMfileContentType() {
return mfileContentType;
}
public void setMfileContentType(String mfileContentType) {
this.mfileContentType = mfileContentType;
}
public File getMfile() {
return mfile;
}
public void setMfile(File mfile) {
this.mfile = mfile;
}
public String getMfileFileName() {
return mfileFileName;
}
public void setMfileFileName(String mfileFileName) {
this.mfileFileName = mfileFileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getAllowedTypes() {
return allowedTypes;
}
public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
struts20登陆和上传的例子
共33个文件
jar:8个
jsp:5个
xml:5个
需积分: 0 9 下载量 123 浏览量
2008-02-06
23:17:49
上传
评论
收藏 5.88MB RAR 举报
温馨提示
用struts20写的登陆和上传的简单例子。
资源详情
资源评论
资源推荐
收起资源包目录
struts2demo1.rar (33个子文件)
struts2demo1
.settings
org.eclipse.jdt.core.prefs 629B
src
log4j.properties 839B
com
struts2
demo
fileUploadAction.java 4KB
package.properties 95B
Logon-validation.xml 506B
Logon.java 713B
HelloWorld.java 503B
struts.xml 1KB
.project 1KB
WebRoot
HelloWorld.jsp 221B
fileupload.jsp 1003B
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 937B
classes
log4j.properties 839B
com
struts2
demo
Logon.class 1KB
package.properties 95B
Logon-validation.xml 506B
fileUploadAction.class 4KB
HelloWorld.class 782B
struts.xml 1KB
lib
commons-io-1.3.1.jar 82KB
commons-logging-1.0.4.jar 37KB
commons-fileupload-1.2.jar 52KB
xwork-2.0.4.jar 445KB
xalan-2.5.1.jar 2.77MB
ognl-2.6.11.jar 164KB
freemarker-2.3.8.jar 784KB
struts2-core-2.0.11.jar 2.22MB
UploadImages
Logon.jsp 473B
Menu.jsp 892B
index.jsp 519B
.mymetadata 309B
.classpath 971B
.myeclipse
共 33 条
- 1
zhangbowu
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0