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

Struts2文件上传和下载

2017-09-19 00:03 483 查看
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>
<package name="csl" namespace="/" extends="struts-default">
<action name="up" class="com.test.UploadAction">
<result name="success">/down.jsp</result>
<result name="input">/index.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/x-pn</param>
<param name="maximumSize">1024*1024</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</action>
</package>
</struts>


UploadAction类

package com.test;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File image;
private String imageFileName;
private String imageContentType;

private String name ;
private String pass;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
@Override
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/uploads");
System.out.println(name+"--->"+pass);
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
System.out.println(file);
FileUtils.copyFile(image,new File(path,imageFileName));
return SUCCESS;
}
}


upload.jsp上传页面

<body>
<form action="up" method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" value="上传"/>
</form>
</body>


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>
<package name="csl" namespace="/" extends="struts-default">
<action name="down" class="com.test.DownloadAction">
<result name="success" type="stream"> <!-- 文件流 -->
<param name="contentType"> text/plain</param>  <!-- 内容类型 -->
<param name="contentDisposition">attachment;fileName="${fileName}"</param>  <!-- 文件名类型 -->
<param name="inputName">inputStream</param>  <!-- 文件流 -->
<param name="bufferSize">1024&
b12f
lt;/param>  <!-- 缓存字节大小 -->
</result>
</action>
</package>
</struts>


down.jsp页面

hljs xml">    <a href="down?fileName=<s:property value="imageFileName" />">下载<s:property value="imageFileName" /></a>
<img alt="" src="<s:property value="'uploads/'+imageFileName"/>">
</body>


DownloadAction类

package com.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
private String fileName;
private InputStream inputStream;

public InputStream getInputStream() throws FileNotFoundException {
String path = ServletActionContext.getServletContext().getRealPath("/uploads");
String file = path +"\\"+ fileName ;
System.out.println(file);
InputStream is = new BufferedInputStream(new FileInputStream(file));
System.out.println(is);
return is;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

@Override
public String execute() throws Exception {
return SUCCESS;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0