您的位置:首页 > 编程语言 > Java开发

struts2的文件上传与下载

2016-06-20 10:52 405 查看

项目中用到了文件上传与下载,现在我用struts2实现:

public class StandardAddAction extends ActionSupport{
/**
*
*/

private File standardfile;
private String standardfileContentType;
private String standardfileFileName;

/**
这三个变量的setter与getter...
*/
@Override
public String execute() throws Exception {
String path1 = (new File(System.getProperty("user.dir"))).toString();
String path2 = path1.replace("bin", "webapps\\ROOT\\StandardFile\\");
System.out.println(path2);

FileInputStream fis = new FileInputStream(getStandardfile());
FileOutputStream fos = new FileOutputStream(path2+getStandardfileFileName());
int len = 0;
byte[]b = new byte[1024];
while((len = fis.read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
fis.close();


解析:

standardfile为页面上上传文件的name的值

standardfileFileName为上传的文件的名称

standardfileContentType为上传的文件的类型

该代码将文件上传到path2路径下,该路径我设置为tomcat的发布程序默认路径,这样在执行下载时,只要打开一个URL就可以。

mission success

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: