js動態生成input,struts2多文件上傳,獲取真實文件名稱

今天在做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;
	  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章