html:
java:
<script>
function changeFileInput(){
var num=$("fileSelect").value;
while($("files").childNodes.length>0){
$("files").removeChild($("files").childNodes[0]);
}
for(var i=0;i<num;i++){
filediv=document.createElement("div");
file=document.createElement("input");
file.type="file";
file.name="file"+i;
file.size="50";
filediv.appendChild(file);
$("files").appendChild(filediv);
}
}
</script>
<td class="td_right">
<div id="files">
<html:file property="file" value="浏览..." size="50" οnkeypress="event.returnValue=false;" ></html:file><br />
</div>
<html:select property="fileSelect" size="1" οnchange="changeFileInput()">
<html:option value="1">1</html:option>
<html:option value="2">2</html:option>
<html:option value="3">3</html:option>
<html:option value="4">4</html:option>
<html:option value="5">5</html:option>
</html:select>
<input title="上传图片" class="button_y" name="upload" οnclick="" type="submit" value="上传图片"><span class="xing">图片小于65k</span>
<html:errors/>
<a href="ledger.portal?action=download&id=fieldId">下载</a>
<input title="图片管理" class="button_y" name="imageManage" οnclick="" type="submit" value="图片管理">
</td>
</tr>
java:
if(request.getParameter("upload")!=null){
System.out.println("---------------------upload---------------------");
// struts多文件上传,JSP中<html:file>的property必须不一样!
System.out.println("----------多文件上传开始-------------");
String realPath=this.getServlet().getServletContext().getRealPath("/");
if(realPath.endsWith("./")){//linux外网访问多了./ why???
realPath=realPath.substring(0, realPath.length()-2);
}
String uploadPath = realPath + "UserFiles"+File.separator+"Image"; //上传文件存放目录
System.out.println("loadpath="+uploadPath);
List<String> list = new ArrayList<String>();
list.add("jpg");
list.add("jpeg");
list.add("gif");
list.add("bmp");
list.add("png");
Hashtable hash=ledgerForm.getMultipartRequestHandler().getFileElements();//获得FormFile的数目,注意JSP中<html:file>的property
System.out.println("hash.size="+hash.size());
int i=0;
for (Enumeration e = hash.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
FormFile formfile = (FormFile) hash.get(key);
String fileName = formfile.getFileName().trim(); //文件名
String contentType=formfile.getContentType();
long size=formfile.getFileSize();
String extName=fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
if(fileName==null||fileName.equals("")){//防没有文件时上传垃圾文件的bug
System.out.println("文件名为空,跳出循环!");
initNewsList(mapping,ledgerForm,request,response);
return mapping.findForward("management");
}
if(!list.contains(extName)){
errors.add("error.image.contentType", new ActionMessage("error.image.contentType"));
this.saveErrors(request, errors);
// initNewsList(mapping,ledgerForm,request,response);
// return mapping.findForward("management");
System.out.println("上传文件中有不符合扩展名要求的文件("+fileName+"),继续下一个上传文件...");
continue;//扩展名不符合要求就跳过他
}
if(size>=1024*65){//65k
errors.add("error.image.size", new ActionMessage("error.image.size"));
this.saveErrors(request, errors);
// initNewsList(mapping,ledgerForm,request,response);
// return mapping.findForward("management");
System.out.println("上传文件中有不符合大小要求的文件("+fileName+"),继续下一个上传文件...");
continue;//文件大小不符合要求就跳过他
}
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName= format.format(date)+"_"+i+"."+extName;
i++;
if (!"".equals(fileName)) { //不同的浏览器传上的文件名可能有区别,有的是全路径的
InputStream is = formfile.getInputStream();
OutputStream os = new FileOutputStream(uploadPath+File.separator+newFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
}
System.out.println("--------------图片放大缩小开始----------------");
String tempPath = realPath + "UserFiles"+File.separator+"temp"; //上传文件存放目录
int w=96;
int h=-20;
if(extName.equals("gif")){//解决生成gif图像时大小为0的bug
extName="png";
}
String oldPath=uploadPath+File.separator+newFileName;//Image文件夹
String newPath=tempPath+File.separator+newFileName;//temp文件夹
try{
Util.resize(oldPath, w, h, newPath, extName);// 图片放大缩小
}catch (Exception ex) {
// TODO: handle exception
System.out.println("出现异常时将原图直接写入temp文件夹,不要缩略了");
InputStream is = formfile.getInputStream();
OutputStream os = new FileOutputStream(newPath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
Util.imageWidth=96;//异常时,当作96像素
Util.imageHeight=96;
}
System.out.println("--------------图片放大缩小结束----------------");
///
System.out.println("--------------保存缩略图/原图开始----------------");
ImageManage imageManage=new ImageManage();
imageManage.setDate(Util.getCurrentDate("yyyyMMddhhmmss"));
imageManage.setImageName(newFileName);//缩略图名=原图名,只是文件夹不一样
imageManage.setUser(Util.userName);
imageManage.setRemark("0");
imageManage.setWidth(Util.imageWidth);//保存图片宽高
imageManage.setHeight(Util.imageHeight);
this.getLedgerService().save(imageManage);
System.out.println("--------------保存缩略图/原图结束----------------");
}
// hash.clear();
System.out.println("----------多文件上传结束-------------");
ledgerForm.setFileSelect("1");
initNewsList(mapping,ledgerForm,request,response);
return mapping.findForward("management");
}