struts2上传下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GZ946/article/details/76472999
                                 struts2下载
action中
   package cn.sdbi.demo1;


import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;


import org.apache.struts2.ServletActionContext;


import sun.misc.BASE64Encoder;


import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("unused")
public class DownLoadDemo extends ActionSupport {
private static final long serialVersionUID = 1L;

private String filename;//文件名

private String contentType;//文件类型


public String getFilename() throws UnsupportedEncodingException{
return filename;
}

public void setFilename( String filename) throws UnsupportedEncodingException {
this.filename =filename;
}
public String getContentType() {
return ServletActionContext.getServletContext().getMimeType(filename);
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
//该方法作为文件下载的入口
    public InputStream getInputStream()throws Exception{
   
    String filePath="/upload/"+filename;
   
    InputStream in=ServletActionContext.getServletContext().getResourceAsStream(filePath);
    //防止中文乱码
    filename=new String(filename.getBytes(),"iso8859-1");
    return in;
   
   
   
    }
    @Override
    public String execute() throws Exception {
   
    return SUCCESS;
    }
    
    
    
}


struts.xml配置中


      <action name="downLoadDemo" class="cn.sdbi.demo1.DownLoadDemo">
  
  
    <result name="success" type="stream">
      <param name="contentType">${contentType}</param>
      <param name="contentDisposition">
       attachment;filename=${filename};
      </param>
      <param name="inputName">inputStream</param>
    </result>
  
  </action>
jsp中
 <a href="/struts1/downLoadDemo.action?filename=文本文档.txt">中文.txt</a>
    <br>
    <a href="/struts1/downLoadDemo.action?filename=test.txt">英文.txt</a>
                                   struts2 上传




action中                                                                                                                                                  package cn.sdbi.demo1;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;






import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;


public class UpLoadDemo extends ActionSupport {
private static final long serialVersionUID = 1L;
    
private String file;
private String fileFileName;
private String fileContentType;
public String getFile() {
return file;
}
public void setFile(String 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;
}

@SuppressWarnings("unused")
@Override
public String execute() throws Exception {

InputStream is=new FileInputStream(file);

String uploadPath=ServletActionContext.getServletContext().getRealPath("/upload");
//设置目标文件
File toFile=new File(uploadPath,this.getFileFileName());
OutputStream os=new FileOutputStream(toFile);
byte[] buffe=new byte[1024];
int len=0;
while((len=is.read(buffe))!=-1){

os.write(buffe);


}
os.close();
is.close();
return SUCCESS;
}
}
                                                                                                                                                                                                                                                                         
struts.xml配置中
 <action name="upLoadDemo" class="cn.sdbi.demo1.UpLoadDemo">
  
  
    <result name="success">
      /result.jsp
    </result>
    <!-- 定义上传出错要跳转的页面 -->
    <result name="input">/error.jsp</result>
  <interceptor-ref name="defaultStack">
     <!--  限制上传文件大小 -->
<param name="fileUpload.maximumSize">4194304</param>
<!--   限制文件扩展名 -->
<param name="fileUpload.allowedExtensions">.txt,.doc,.jpg,.png,.mp3</param>
<!--   限制上传文件类型 -->
<param name="fileUpload.allowedTypes">text/plain,audio/mpeg,application/msword,image/jpeg</param>
</interceptor-ref>


  </action>


jsp中
<s:form action="upLoadDemo" method="post" enctype="multipart/form-data">
      <s:file name="file" label="上传文件"></s:file>
      <s:submit value="上传"></s:submit>
    </s:form>









阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页