zoukankan      html  css  js  c++  java
  • Struts2多文件上传

    Struts2多文件上传

     1 package com.tcf.action;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 import com.opensymphony.xwork2.ActionSupport;
    10 
    11 public class UploadesAction extends ActionSupport {
    12     private File[] file;
    13     private String[] fileContentType;
    14     private String[] fileFileName;
    15     private String savePath;
    16     public File[] getFile() {
    17         return file;
    18     }
    19 
    20     public void setFile(File[] file) {
    21         this.file = file;
    22     }
    23 
    24     public String[] getFileContentType() {
    25         return fileContentType;
    26     }
    27 
    28     public void setFileContentType(String[] fileContentType) {
    29         this.fileContentType = fileContentType;
    30     }
    31 
    32     public String[] getFileFileName() {
    33         return fileFileName;
    34     }
    35 
    36     public void setFileFileName(String[] fileFileName) {
    37         this.fileFileName = fileFileName;
    38     }
    39 
    40     public String getSavePath() {
    41         return savePath;
    42     }
    43 
    44     public void setSavePath(String savePath) {
    45         this.savePath = savePath;
    46     }
    47 
    48     public String upload(){
    49         byte[] buf = new byte[1024];
    50         try {
    51             for(int i = 0;i<file.length;i++){
    52                 FileInputStream fis = new FileInputStream(file[i]);//
    53                 FileOutputStream fos = new FileOutputStream(savePath+"/"+fileFileName[i]);//放到哪里
    54                 int len = 0;
    55                 while((len=fis.read(buf))!=-1){
    56                     fos.write(buf,0,len);
    57                 }
    58                 //刷新缓存
    59                 //fos.flush();
    60                 fos.close();
    61                 fis.close();
    62             }
    63         } catch (FileNotFoundException e) {
    64             // TODO Auto-generated catch block
    65             e.printStackTrace();
    66         } catch (IOException e) {
    67             // TODO Auto-generated catch block
    68             e.printStackTrace();
    69         }
    70         return SUCCESS;
    71     }
    72 }

    struts.xml

    1 <action name="uploades" class="com.tcf.action.UploadesAction" method="upload">
    2     <param name="savePath">E:/temp</param>
    3     <result>uploadSuccess.jsp</result>
    4 </action>

    upload.jsp

    1 <s:form action="uploades.action" enctype="multipart/form-data" method="post">
    2         <s:file name="file" />
    3         <s:file name="file" />
    4         <s:file name="file" />
    5         <s:submit></s:submit>
    6 </s:form>
  • 相关阅读:
    程序员的7中武器
    需要强化的知识
    微软中国联合小i推出MSN群Beta 不需任何插件
    XML Notepad 2006 v2.0
    Sandcastle August 2006 Community Technology Preview
    [推荐] TechNet 广播 SQL Server 2000完结篇
    《太空帝国 4》(Space Empires IV)以及 xxMod 英文版 中文版 TDM Mod 英文版 中文版
    IronPython 1.0 RC2 更新 1.0.60816
    Microsoft .NET Framework 3.0 RC1
    《Oracle Developer Suite 10g》(Oracle Developer Suite 10g)V10.1.2.0.2
  • 原文地址:https://www.cnblogs.com/yunwei1237/p/5899652.html
Copyright © 2011-2022 走看看