提交页面:
<form name="uploadform" method="post" action="./app/upLoad/saveUpload.jsp" ENCTYPE="multipart/form-data">
<table>
<thead>
<tr>
<th><h2><b><font color='red'>生成考核排名序列</font></b></h2></th>
</tr>
</thead>
<tr>
<td>
上月考核成绩:<input name="<%=ComXlpmEnum.lastMonthScore_Key %>" size="40" type="file">
</td>
<td>
<input name="upload" type="submit" value="开始上传"/>
</td>
</tr>
</table>
</form>
后台上传页面:
String uploadFilePrePath = request.getSession().getServletContext().getRealPath("/")+"uploadFiles\\";
com.jspsmart.upload.SmartUpload smartUpload=new com.jspsmart.upload.SmartUpload();
smartUpload.initialize(pageContext);
String fileName= "";
StringBuffer fileNameStr= new StringBuffer();
try{
smartUpload.setAllowedFilesList( "xls"); //这里可以限制上传类型
smartUpload.setMaxFileSize(500000); // 限制每个上传文件的最大长度。
boolean isfile=true;
smartUpload.upload();
com.jspsmart.upload.File myfile=null;
if(smartUpload.getFiles().getCount()> 0){
for(int i=0;i <smartUpload.getFiles().getCount();i++){
myfile = smartUpload.getFiles().getFile(i);
String itemFieldName = myfile.getFieldName();
if(itemFieldName != null && itemFieldName.equals(ComXlpmEnum.lastMonthScore_Key)){
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yy-MM");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH,-1);//减一个月,变为下月的1号
String preMounth=sdf.format(lastDate.getTime());
fileName = uploadFilePrePath+preMounth+".xls";
}else{
fileName = uploadFilePrePath+myfile.getFieldName()+"."+myfile.getFileExt();
}
out.println(fileName);
out.println("<br />");
if(fileNameStr.length() < 1){
fileNameStr.append(" ' "+fileName+ " ' ");
}else{
fileNameStr.append(", ' "+fileName+ " ' ");
}
myfile.saveAs(fileName);
}
%>