各位大侠们,求教,小弟做这个图片批量上传怎么就是不成功,完整的代码在下面,大侠看看错误在哪里。谢谢了。
jsp页面代码:
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html> <head> <title>upload index</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0">
<!-- begin the important code --> <link href="scripts/default.css" rel="stylesheet" type="text/css" /> <link href="scripts/uploadify.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="scripts/swfobject.js"></script> <script type="text/javascript" src="scripts/jquery.uploadify.v2.1.4.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#uploadify").uploadify({ 'uploader' : 'scripts/uploadify.swf', // uploadify.swf 文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,
//点击后淡出打开文件对话框,默认值:uploadify.swf。 'script' : 'scut/dyh/action/MultiUploadAction', //后台处理程序的相对路径 。默认值:uploadify.php
'scriptData': {'x':$("#nodeid").attr("value")}, 'method':'GET', 'cancelImg' : 'images/cancel.png', 'folder' : 'uploads', //上传文件存放的目录 'queueID' : 'fileQueue', //文件队列的ID,该ID与存放文件队列的div的ID一致。 'auto' : false, //设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 'multi' : true, //设置为true时可以上传多个文件 'simUploadLimit' : 100, //一次同步上传的文件数目
'sizeLimit' : 19871202, //设置单个文件大小限制,单位为byte 'queueSizeLimit' : 100, //当允许多文件生成时,设置选择文件的个数,默认值:999 'fileDesc' : '支持格式:jpg/gif/jpeg/png/bmp.', //如果配置了以下的'fileExt'属性,那么这个属性是必须的 'fileExt' : '*.jpg;*.gif;*.jpeg;*.png',//允许的格式
'buttonText' : '' ,
'buttonImg' : 'images/select.jpg',
'width' : '152',
'height' : '40',
onComplete: function (event, queueID, fileObj, response, data) { var value = response ; alert("success back value"+value); alert("文件:" + fileObj.name + "上传成功"); }, onError: function(event, queueID, fileObj) { alert("文件:" + fileObj.name + "上传失败"); }, onCancel: function(event, queueID, fileObj){ alert("取消了" + fileObj.name); } }); }); </script> </head> <body><div id="fileQueue"></div><input type="file" name="uploadify" id="uploadify" /><p><a href="javascript:jQuery('#uploadify').uploadifyUpload()"><img src="images/upload.jpg"></a> <a href="javascript:jQuery('#uploadify').uploadifyClearQueue()"><img src="images/deleteAll.jpg"/></a></p><input type="text" name="nodeid" id="nodeid" value="inputtxtvalue" /></body></html>
Action 代码:package scut.dyh.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class MultiUploadAction extends ActionSupport{ private static final long serialVersionUID = -3001761530721133968L;
private List<File> uploadify;
private List<String> uploadifyFileName;
private List<String> uploadifyContentType;
public String upload() throws Exception
{
String savePath = ServletActionContext.getServletContext().getRealPath(""); //获取项目根路径
savePath = savePath + "/uploads/"; //文件的保存路径
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8"); //务必,防止返回文件名是乱码
//循环处理上传文件List;
for(int i=0;i<uploadify.size();i++){
FileOutputStream fos=new FileOutputStream(savePath+this.getFileName(this.uploadifyFileName.get(i)));
FileInputStream fis=new FileInputStream(uploadify.get(i));
byte[] buffer=new byte[1024];
int length=0;
while((length=fis.read(buffer))>0)
{
fos.write(buffer,0,length);
}
response.getWriter().print(uploadifyFileName.get(i) + " 上传成功");//向页面端返回结果信息
}
return null; //这里不需要页面转向,所以返回空就可以了
}
/**
* 重新得到保存文件的文件名
* @param fileName
* @return
*/
public String getFileName(String fileName)
{
String extName = ""; //保存文件拓展名
String newFileName = ""; //保存新的文件名
String nowTimeStr = ""; //保存当前时间
SimpleDateFormat sDateFormat;
Random r = new Random();
//生成随机文件名:当前年月日时分秒+五位随机数(为了在实际项目中防止文件同名而进行的处理)
int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000; //获取随机数
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //时间格式化的格式
nowTimeStr = sDateFormat.format(new Date()); //当前时间
if (fileName.lastIndexOf(".") >= 0){
extName = fileName.substring(fileName.lastIndexOf("."));
}
newFileName = nowTimeStr + rannum + extName; //文件重命名后的名字
return newFileName;
}
public List<File> getUploadify() {
return uploadify;
}
public void setUploadify(List<File> uploadify) {
this.uploadify = uploadify;
}
public List<String> getUploadifyFileName() {
return uploadifyFileName;
}
public void setUploadifyFileName(List<String> uploadifyFileName) {
this.uploadifyFileName = uploadifyFileName;
}
public List<String> getUploadifyContentType() {
return uploadifyContentType;
}
public void setUploadifyContentType(List<String> uploadifyContentType) {
this.uploadifyContentType = uploadifyContentType;
}
}
Strusts.xml:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myPackage" extends="struts-default">
<action name="uploadUtil" class="scut.dyh.action.MultiUploadAction" method="upload">
</action>
</package>
</struts>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货