js动态生成input,struts2多文件上传,获取真实文件名称

本文介绍了在Struts2中进行多文件上传处理时,获取上传文件名称的方法。指出Struts上传文件默认获取的是XXX.tmp后缀的名称,还说明了JSP代码、JS生成input以及后台接收的相关内容,包括后台属性设置和路径判断创建等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在做struts2的多文件上传处理,但是需要用到上传的文件名称,struts上传的文件默认获取的是 XXX.tmp后缀的名称,经过查询资料可以用以下方法获取:

注意:如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为

    private File xxx;
    private String xxxFileName;

一、JSP代码

<form action="<%=request.getContextPath()%>/archiveToSave.action" method="post" enctype="multipart/form-data"  name="form2" id="form2">
   <table width="100%">
   <tr class="trClassT">
  		<td align="center">归档信息</td>
        <td align="right">
        <img src="<%=request.getContextPath() %>/images/closediv.gif" style="cursor:pointer;" height="16" width="16" onclick="history.go(-1);"/>
        </td>
   </tr>
    <input type="hidden" id="psId" name="psId"   readonly="readonly" value="<s:property value="vfp.psId"/>"/>
    <tr>
     <td>姓名:</td>
     <td><input type="text" id="psName" name="psName" readonly="readonly" value="<s:property value="vfp.psName"/>"/></td>
    </tr>
    <tr>
     <td>身份证号:</td>
     <td><input type="text" id="psSfId" name="psSfId" readonly="readonly" value="<s:property value="vfp.psSfId.substring(0,8)+'***'+vfp.psSfId.substring(vfp.psSfId.length()-5)"/>"/></td>
    </tr>
    
    <div>
    <tr>
     <td >附件:</td> 
     <td >
     <input id="importFiles1" type="file" name="importFiles" onchange="changefiles();"/>
     <input type="button" value="添加上传文件" onclick="addinput()"/>
     </td>
    </tr>
    <tr><td></td>
    <td><div id="file"></div></td>
    </tr>
    
    <div>
   </table>
   <input type="button" value="确定" class="btnClass" onclick="javascript:submitForm();"/>
</form>

 

二、JS生成input

<script language="javascript">
function submitForm(){
$("#form2").submit();
};
function addinput(){
    var count = document.getElementById("file").getElementsByTagName("input").length/2+1;
    if(count<5){
    var div = document.getElementById("file");
    var input = document.createElement("input");
    input.type="file";
    input.name="importFiles";
    input.id="importFiles"+count;
    input.onchange = function changefiles(){
    var self = this.id;
    var File=document.getElementById(""+self);
    var pdfFile=File.value;
	if(pdfFile==null||pdfFile==""){
		alert("请选择要导入的pdf文件");
		return;
	}
	var arr = pdfFile.split(".");  
    var extname = arr[arr.length-1];  
    extname = extname.toLowerCase();
    if(extname != "pdf"){  
         alert("请选择pdf格式文件!");
         File.value="";
         return false;  
    } 
    
    };
    var del = document.createElement("input");
    del.type="button";
    del.value="删除";
    del.onclick = function d(){
        this.parentNode.parentNode.removeChild(this.parentNode);
    };
    var innerdiv = document.createElement("div");
    innerdiv.appendChild(input);
    innerdiv.appendChild(del);
    div.appendChild(innerdiv);
    }else{
     alert("最多上传5个文件!");  
     return false;  
    }
}

function changefiles(){
var pdfFile=document.getElementById("importFiles1").value;
	if(pdfFile==null||pdfFile==""){
		alert("请选择要导入的pdf文件");
		return;
	}
	var arr = pdfFile.split(".");  
    var extname = arr[arr.length-1];  
    extname = extname.toLowerCase();
    if(extname != "pdf"){  
         alert("请选择pdf格式文件!");  
         document.getElementById("importFiles1").value="";
         return false;  
    } 
}
</script>

 

三、后台接收

private File [] importFiles;
private String[] importFilesFileName;

getset方法省略。

/**
	 * 归档保存
	 * @return
	 * @throws Exception
	 */
	public String archiveToSave()throws Exception{
		boolean re=UploadUtils.uploadFileByFileAndUrl(importFiles,importFilesFileName,psId);
		if(re){	
		this.vfp=(VFp)super.getInPack().getFamilyInterfaces().getEntityBySerID(VFp.class, this.psId);
		String u=ServletActionContext.getServletContext().getRealPath("/")+"TempFolder/archive/"+psId+"/";
	    File file = new File(u);
		File[] fs= file.listFiles();
		for(int i = 0;i<fs.length;i++) {
			FileNameList.add(fs[i].getName());
			}
		return "archiveToSaveOk";}
		else return "archivefail";
	}

 判断路径是否存在,不存在创建父目录,子目录

  if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}
        if(!file.exists()){file.mkdir();}

 /**
   * 上传文件  
   * @param sourceFile 页面传入的文件
   * @param url 文件保存路径
   * @return
   */
  public static boolean uploadFileByFileAndUrl(File[] importFiles, String[] importFilesFileName,String psId){
	  if(importFiles==null || importFiles.length==0){ return false;}
	    String u=ServletActionContext.getServletContext().getRealPath("/")+"TempFolder/archive/"+psId+"/";
	    File file = new File(u);
	    if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}
        if(!file.exists()){file.mkdir();}
	
	  try{
		 for (int i = 0; i < importFiles.length; i++) {
		  if(importFiles[i]!=null){ 
		  String n= importFilesFileName[i];
		  String url =u+n+".pdf";  
		  InputStream is = new FileInputStream(importFiles[i]);  // 设置目标文件   
		  File toFile = new File(url);  // 创建一个输出流   
		  OutputStream os = new FileOutputStream(toFile);   //设置缓存   
		  byte[] buffer = new byte[1024];   
		  int length = 0;    //读取myFile文件输出到toFile文件中   
		  while ((length = is.read(buffer)) > 0) {   
			  os.write(buffer, 0, length);   
		  }   
		  is.close();   //关闭输入流        
		  os.close();   //关闭输出流  
		  }
		}
		  return true;
	  }catch(Exception e){
		  return false;
	  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值