多图片(文件)上传下载

UploadAction.java

public class UploadAction{

public String addAct(){

StringBuffer sbf=new StringBuffer();

if(file!=null  && file.length > 0){
   for(int i=0;i<file.length;i++){
    try{
     String filename=fileFileName[i];
     String fileExt = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
     String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
     InputStream is = new FileInputStream(file[i]);  
     File destFile = new File(root, newFileName);
     OutputStream os = new FileOutputStream(destFile);  
     byte[] buffer = new byte[50000];  
     int length = 0;  
     while (-1 != (length = is.read(buffer))) {  
      os.write(buffer, 0, length);  
     }
     sbf.append(newFileName);
     if(i < file.length-1){
      sbf.append(",");
     }
    }catch(Exception e){
     log.debug("上传文档失败");
    }
    
   }
  }

 

 

 

}

private File[] file;


 

/*

     * 这里定义的fileFileName一定要是xxxFileName的形式,否则无法取到文件的文件名.

     * 其中xxx必须与上面定义的File类型的变量一致,如果上面定义的是File img,那么这里就

    * 定义为String imgFileName

    */ 

 private String[] fileFileName;


 

/*

     * 这里定义的是文件的类型,如果不需要获取文件类型的话,可以不定义.

     *  命名规则跟xxxFileName类似,这里一定要定义成xxxContentType形式.

     */

 private String[] fileContextType;

//下载文件

public InputStream getDownExclFile() {
  File file = new File(PluginPropertiesLoader.getPluginResource()+"/addins/excel/"+fliename);
  
  try {
   InputStream is = new FileInputStream(file);
   return is;
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  }
  return null;
 }
 public void setDownExclFile(InputStream downExclFile) {
  this.downExclFile = downExclFile;
 }
 
 public File[] getFile() {
  return file;
 }
 public String[] getFileFileName() {
  return fileFileName;
 }
 public String[] getFileContextType() {
  return fileContextType;
 }

public void setFile(File[] file) {
  this.file = file;
 }
 public void setFileFileName(String[] fileFileName) {
  this.fileFileName = fileFileName;
 }
 public void setFileContextType(String[] fileContextType) {
  this.fileContextType = fileContextType;
 }

 

}

jsp页面

<form  action="harmless_addInfo.html" method="post" onSubmit="return Validator.Validate(this)" enctype="multipart/form-data">

 <input type="file" name="file" style="width:240px;" οnfοcus="javascript:setfocus(this);" οnblur="javascript:setblur(this);"  seq="1" class="seq" />
      <a href="#"  οnclick="addtr()"> <img src="images/edit_add.png"></img>&nbsp;&nbsp;添加</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br/>
     
      </p>
      <p>(支持jpg、png或gif格式,500x500像素以内,1M以下)</p>

</form>

注意:<form>中的“enctype="multipart/form-data"”不能少,method="post",<input>中的name必须要和action中的定义的file要一样,如:<input type="file" name="file">在action中private File[] file;

js (添加多个上传框,可以删除)

function addtr(){
  var tdNameLast=$("input[class=seq]:last");
  var seqStr=tdNameLast.attr("seq");
  var seq=parseInt(seqStr);
  $("#file").append('<span  id="'+(seq+1)+'"><input type="file" name="file" style="width:240px;" οnfοcus="javascript:setfocus(this);" οnblur="javascript:setblur();"  class="seq"  seq="'+(seq+1)+'"/><a href="#"  οnclick="addtr()"> <img src="images/edit_add.png"  ></img> &nbsp;&nbsp;添加</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="#"  οnclick="deltr(this)" seq="'+(seq+1)+'" ><img src="images/edit_remove.png"></img>&nbsp;&nbsp;删除</a></span><br/>');
  }
  function deltr(obj){
  alert(obj);
        var seq = $(obj).attr("seq");
        alert("seq------------------------------"+seq);
  $("span[id="+seq+"]").remove();
  }

struts.xml

自行配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值