欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中struts2实现简单的文件上传与下载

程序员文章站 2024-03-11 12:11:49
一、struts2文件上传  struts2的文件上传实现非常简单,只需要简单几步就可完成; 注意: (1)文件上传的struts2标签为:

一、struts2文件上传
 struts2的文件上传实现非常简单,只需要简单几步就可完成;
注意:
(1)文件上传的struts2标签为:<s:file name="" label="上传"/>
(2)文件上传的前提是表单属性method="post" enctype="multipart/form-data";
(3)web应用中必须包含common-fileupload.jar和common-io.jar,因为struts2默认上传解析器使用的是jakarta;
(4)可以在struts.xml中配置最大允许上传的文件大小:<constant name="struts.multipart.maxsize" value="....."/>,默认为2m;

1.普通文件上传
 实现规则:
(1)在jsp中设定表单控件<s:file name="upload" label="上传"/>
(2)在action中定义属性:
private file upload;        //包含文件内容
private string uploadfilename;    //上传文件的名称;
private string uploadcontenttype;    //上传文件的mime类型;
这些属性都会随着文件的上传自动赋值;
(3)在execute()中完成写入磁盘功能;

代码示例:
 upload01action.java

package org.upload.action; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
 
import org.apache.struts2.servletactioncontext; 
 
import com.opensymphony.xwork2.actionsupport; 
 
public class upload01action extends actionsupport { 
  private file upload; 
  private string uploadfilename; 
  private string uploadcontenttype; 
  private string name; 
  public string execute()throws exception{ 
    string path = servletactioncontext.getservletcontext().getrealpath("/web-inf/upload"); 
    string filename = path+file.separator+name; 
    fileinputstream in = new fileinputstream(upload); 
    fileoutputstream out = new fileoutputstream(filename); 
    byte[]b = new byte[1024]; 
    int len = 0; 
    while((len=in.read(b))>0){ 
      out.write(b,0,len); 
    } 
    out.close(); 
    return success; 
  } 
 
  public file getupload() { 
    return upload; 
  } 
 
  public void setupload(file upload) { 
    this.upload = upload; 
  } 
 
  public string getuploadfilename() { 
    return uploadfilename; 
  } 
 
  public void setuploadfilename(string uploadfilename) { 
    this.uploadfilename = uploadfilename; 
  } 
 
  public string getuploadcontenttype() { 
    return uploadcontenttype; 
  } 
 
  public void setuploadcontenttype(string uploadcontenttype) { 
    this.uploadcontenttype = uploadcontenttype; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
   
   
} 

struts.xml

 <?xml version="1.0" encoding="utf-8" ?> 
<!doctype struts public 
  "-//apache software foundation//dtd struts configuration 2.0//en" 
  "http://struts.apache.org/dtds/struts-2.0.dtd"> 
 
<struts> 
  <constant name="struts.devmode" value="true" /> 
  <constant name="struts.custom.i18n.resources" value="message"></constant> 
  <package name="default" namespace="/" extends="struts-default"> 
    <action name="upload0*" class="org.upload.action.upload0{1}action"> 
      <param name="name">1.jpg</param> 
      <result>/{1}.jsp</result> 
    </action> 
  </package> 
</struts> 

1.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<%@taglib prefix="s" uri="/struts-tags" %> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head>   
  <title>my jsp '1.jsp' starting page</title> 
 </head> 
  
 <body>  
  <s:form action="upload01" method="post" enctype="multipart/form-data"> 
    <s:file name="upload" label="上传"></s:file> 
    <s:submit value="上传"></s:submit> 
  </s:form> 
 </body> 
</html> 

 2.利用拦截器进行过滤
手动实现过滤的方式非常简单,就是利用输入校验的方式进行过滤,即在validate()中进行过滤;
而这里要讲的拦截器方式是很好的方式,只需要在配置文件中配置,灵活性很好,能够限制文件的类型、文件的大小;如果上传的文件不符合要求,则返回input逻辑视图; 

配置拦截器步骤: 

(1)文件上传的拦截器为fileupload;
(2)需要给定参数allowedtypes、maximumsize;
(3)在fileupload拦截器后,需要添加<interceptor-ref name="defaultstack"/> 

代码示例:
由于通过拦截器进行过滤只需要配置struts.xml,因此这里只给出struts.xml的配置

<?xml version="1.0" encoding="utf-8" ?> 
<!doctype struts public 
  "-//apache software foundation//dtd struts configuration 2.0//en" 
  "http://struts.apache.org/dtds/struts-2.0.dtd"> 
 
<struts> 
  <constant name="struts.devmode" value="true" /> 
  <constant name="struts.custom.i18n.resources" value="message"></constant> 
  <package name="default" namespace="/" extends="struts-default"> 
    <action name="upload0*" class="org.upload.action.upload0{1}action"> 
      <interceptor-ref name="fileupload"> 
        <param name="allowedtypes">image/jpeg,image/gif</param> 
        <param name="maximumsize">1024*1024</param> 
      </interceptor-ref> 
      <interceptor-ref name="defaultstack"></interceptor-ref> 
      <param name="name">1.jpg</param> 
      <result>/{1}.jsp</result> 
      <result name="input">/{1}.jsp</result> 
    </action> 
  </package> 
</struts> 

我们还需要配置文件上传失败后的错误提示信息,我们需要在全局国际化资源文件中配置:

struts.messages.error.content.type.not.allowed=文件类型不匹配 
 
struts.messages.error.file.too.large=文件太大 

二、struts2文件下载
 我们在学习servlet和http协议时已经可以实现文件下载,即写content-disposition头即可,struts2的实现原理也是这个,但是提供了更好的封装性;
struts2的stream结果类型专门用于实现文件下载;
(1)struts.xml中配置stream结果类型,并配置contenttype、contentdisposition、buffersize参数即可,模板:

<action name="download" class=""> 
  <result type="stream" name="success"> 
    <param name="contenttype"></param> 
    <param name="contentdisposition">attachment;filename=""</param> 
    <param name="buffersize">4096</param> 
  </result> 
</action> 

(2)在action中创建public inputstream getinputstream()throws exception;方法,此方法用于获得下载文件的输入流;

downloadaction.java

package org.download.action; 
 
import java.io.inputstream; 
 
import org.apache.struts2.servletactioncontext; 
 
import com.opensymphony.xwork2.actionsupport; 
 
public class downloadaction extends actionsupport { 
  public inputstream getinputstream()throws exception{ 
     
    return servletactioncontext.getservletcontext().getresourceasstream("/web-inf/upload/1.jpg"); 
     
  } 
  public string execute()throws exception{ 
    return success; 
  } 
} 

struts.xml

<action name="download" class="org.download.action.downloadaction"> 
      <result type="stream" name="success"> 
        <param name="contenttype">image/jpeg</param> 
        <param name="contentdisposition">attachment;filename="1.jpg"</param> 
        <param name="buffersize">4096</param> 
      </result> 
    </action> 

 就可以完成下载。

java中struts2文件上传下载的实现就为大家介绍到这里,谢谢大家的阅读。