java action 上传文件_java框架篇---struts之文件上传和下载

Struts2文件上传

Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传。上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的目录,以确保数据不丢失。

请注意,服务器有一个安全策略可能会禁止写到目录以外的临时目录和属于web应用的目录。

在Struts中的文件上传是通过预先定义的拦截文件上传拦截器这是可通过org.apache.struts2.interceptor.FileUploadInterceptor类的defaultStack中的一部分。仍然可以使用在struts.xml中设置各种参数,我们将在下面看到。

视图文件index.jsp

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

">

My JSP 'index.jsp' starting page

你要上传的文件

在上面的例子中值得注意几点说明。首先,表单的enctype属性设置为multipart/ form-data。这应该是设置为使得处理文件上传文件上传。下一个点值得注意的是表单的 action方法上传和文件上传字段的名称 - myFile。我们需要这些信息创建操作方法和struts配置。

接下来让我们创建一个简单的 jsp 文件的success.jsp 结果显示我们的文件上传的情况下成功。

/p>

"http://www.w3.org/TR/html4/loose.dtd">

文件上传

创建action类: 处理上传文件,

packagecom.oumyye.FileUpload;importjava.io.File;importjava.io.IOException;importorg.apache.commons.io.FileUtils;importcom.opensymphony.xwork2.ActionSupport;public class FileUploadAction extendsActionSupport{privateFile myFile;privateString myFileContentType;privateString myFileFileName;privateString destPath;/****/

private static final long serialVersionUID = 1L;

@Overridepublic String execute() throwsException {//TODO Auto-generated method stub

destPath = "e:/upload/";try{

System.out.println("Src File name: " +myFile);

System.out.println("我的文件名"+myFileFileName);

System.out.println("我的文件类型"+myFileContentType);

File destFile= newFile(destPath, myFileFileName);

FileUtils.copyFile(myFile, destFile);

}catch(IOException e){

e.printStackTrace();returnERROR;

}returnSUCCESS;

}publicFile getMyFile() {returnmyFile;

}public voidsetMyFile(File myFile) {this.myFile =myFile;

}publicString getMyFileContentType() {returnmyFileContentType;

}public voidsetMyFileContentType(String myFileContentType) {this.myFileContentType =myFileContentType;

}publicString getMyFileFileName() {returnmyFileFileName;

}public voidsetMyFileFileName(String myFileFileName) {this.myFileFileName =myFileFileName;

}publicString getDestPath() {returndestPath;

}public voidsetDestPath(String destPath) {this.destPath =destPath;

}public static longgetSerialversionuid() {returnserialVersionUID;

}

}

配置文件:

可以使用恒定的标签在应用程序 struts.xml文件,像我一样改变要上传的文件的最大大小。让我们有我们的在struts.xml如下:

/success.jsp

/index.jsp

text/plain

attachment;fileName="${fileName}"

downloadFile

1024

以下是web.xml文件中的内容,与Struts2的基本配置一样

Struts2_1000_FileUpload

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

*.action

上述代码即可完成文件上传

文件下载

视图文件filedownload.jsp

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

">

文件下载

文件下载内容:


Dream.jpg:点击下载

创建action类: 处理上传文件,

packagecom.oumyye.action;importjava.io.InputStream;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;//文件下载

public class FileDownload extendsActionSupport{privateString fileName;publicString getFileName() {returnfileName;

}public voidsetFileName(String fileName) {this.fileName =fileName;

}//返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流

public InputStream getDownloadFile() throwsException

{this.fileName = "hello.jpg";//获取资源路径

return ServletActionContext.getServletContext().getResourceAsStream("upload/"+this.fileName) ;

}

@Overridepublic String execute() throwsException {returnSUCCESS;

}

}

配置文件同上

下载时可能会出现错误

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the tag specified for this action.

可能的原因:1.文件路径不对,根本就没有取到文件。这种情况下,可以将获得InputStream的那条语句放在system.out.println()中输出一下,若为null,那就是路径不对了,或者说得准确些就根本没有找到文件。

2.在action中没有写配置文件中""后面属性的那个get方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值