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