读书人

Struts2文件下传与上载

发布时间: 2012-08-24 10:00:21 作者: rapoo

Struts2文件上传与下载

Struts2下载:

?

struts.xml:

<action name="case_exportCases"  method="exportCases">   <param name="sessionGroup">cases</param>   <result name="success" type="stream">   <param name="contentType">text/plain</param>             <param name="inputName">inputStream</param>               <param name="contentDisposition">attachment;filename="sims.sql"</param>             <param name="bufferSize">4000</param>   </result>  </action>

?

?

?

参数含义:

contentType

内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片

inputName

下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream 的属性需要编写getInputStream()方法

contentDisposition

文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:

attachment;filename="struts2.txt" ,表示文件下载的时候保存的名字应为struts2.txt 。如果直接写filename="struts2.txt" ,那么默认情况是代表inline ,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"

bufferSize

下载缓冲区的大小

?

Action:

?

public class SimsCaseinfoAction extends BaseEditAction{private InputStream inputStream;  //这里与XML里配置的inputName相对应public FileInputStream getInputStream()  throws IOException {  return new java.io.FileInputStream("D:\\apache6-working\\webapps\\sims\\sims.sql");//从系统磁盘文件读取数据   } public String exportCases() throws Exception {                   return SUCCESS;          } }

?

?

Struts2上传:

?

?? Action:

?

public class SimsCaseinfoAction extends BaseEditAction{      private static final int BUFFER_SIZE = 16 * 1024 ;     private File clientFile;     private String clientFileFileName;     //struts2默认可以获得文件名和文件类型,格式为xxxFileName,xxxContentType     private String clientFileContentType;public String uploadFile() throws Exception {             //把客户端上传的文件上传到服务器上                String fileName = this.getClientFileFileName();     File serverFile = new File(ServletActionContext.getServletContext().getRealPath("/import") + "/" + fileName);     String flag = "";     String str =  fileName.substring(fileName.indexOf(".")+1);     if(!str.equals("sql")){          getRequest().setAttribute("message", "error");     return SUCCESS;     }     copy(clientFile, serverFile);     }    private static void copy(File src, File dst)  {        try  {           InputStream in = null ;           OutputStream out = null ;            try  {                               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);                byte [] buffer = new byte [BUFFER_SIZE];                while (in.read(buffer) > 0 )  {                   out.write(buffer);               }             } finally  {                if ( null != in)  {                   in.close();               }                  if ( null != out)  {                   out.close();               }            }         } catch (Exception e)  {           e.printStackTrace();       }    } public File getClientFile() {return clientFile;}public void setClientFile(File clientFile) {this.clientFile = clientFile;}public String getClientFileFileName() {return clientFileFileName;}public void setClientFileFileName(String clientFileFileName) {this.clientFileFileName = clientFileFileName;}public String getClientFileContentType() {return clientFileContentType;}public void setClientFileContentType(String clientFileContentType) {this.clientFileContentType = clientFileContentType;}

??

读书人网 >软件架构设计

热点推荐