WebUploader 请求action没反应,求大神指导,之前用Struts2的文件上传是没问题的!
Dom:<divid="picker">选择文件</div><buttonid="ctlBtn"class="btnbtn-defaultbtn-sm">开始上传</bu...
Dom:
<div id="picker">选择文件</div>
<button id="ctlBtn" class="btn btn-default btn-sm">开始上传</button>
初始化:
var uploader = WebUploader.create({
swf:'../js/webuploader-0.1.5/Uploader.swf',
server: 'MultiFileUpload.action',
pick: {
id:'#picker',
multiple: true
},
resize: false,
fileNumLimit:'5',
fileSingleSizeLimit: '104857600'
});
Struts配置:
<action name="MultiFileUpload" class="com.integration.action.fileupdown.MultiFileUploadAction">
<result name="input">upload.sjsp</result>
<result name="success">multiFileSuccess.jsp</result>
<param name="uploadDir">uploadFiles</param>
</action>
action:
public class MultiFileUploadAction extends ActionSupport {
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
private String uploadDir;
省略掉了geter seter
@Override
public String execute() throws Exception {
String newFileName = null;
// 循环处理多个上传文件
for (int i = 0; i < file.length; i++) {
// 得到当前时间开始流逝的毫秒数,将这个毫秒数做为新的文件名
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String time=format.format(new Date());
int index = fileFileName[i].lastIndexOf(".");
// 得到保存上传文件的真实路径
String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir = new File(path);
// 如果目录不存在就创建它
if (!dir.exists()) {
dir.mkdir();
}
// 判断上传文件是否有扩展名
if (index != -1) {
// newFileName = now + fileFileName[i].substring(index);
newFileName = fileFileName[i].substring(0,index)+"("+time+")"+fileFileName[i].substring(index);
} else {
newFileName = fileFileName[i]+"("+time+")";
}
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
// 读取保存在临时目录下的上传文件,写入到新的文件中
try {
FileInputStream fis = new FileInputStream(file[i]);
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(new File(dir, newFileName));
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return SUCCESS;
} 展开
<div id="picker">选择文件</div>
<button id="ctlBtn" class="btn btn-default btn-sm">开始上传</button>
初始化:
var uploader = WebUploader.create({
swf:'../js/webuploader-0.1.5/Uploader.swf',
server: 'MultiFileUpload.action',
pick: {
id:'#picker',
multiple: true
},
resize: false,
fileNumLimit:'5',
fileSingleSizeLimit: '104857600'
});
Struts配置:
<action name="MultiFileUpload" class="com.integration.action.fileupdown.MultiFileUploadAction">
<result name="input">upload.sjsp</result>
<result name="success">multiFileSuccess.jsp</result>
<param name="uploadDir">uploadFiles</param>
</action>
action:
public class MultiFileUploadAction extends ActionSupport {
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
private String uploadDir;
省略掉了geter seter
@Override
public String execute() throws Exception {
String newFileName = null;
// 循环处理多个上传文件
for (int i = 0; i < file.length; i++) {
// 得到当前时间开始流逝的毫秒数,将这个毫秒数做为新的文件名
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String time=format.format(new Date());
int index = fileFileName[i].lastIndexOf(".");
// 得到保存上传文件的真实路径
String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir = new File(path);
// 如果目录不存在就创建它
if (!dir.exists()) {
dir.mkdir();
}
// 判断上传文件是否有扩展名
if (index != -1) {
// newFileName = now + fileFileName[i].substring(index);
newFileName = fileFileName[i].substring(0,index)+"("+time+")"+fileFileName[i].substring(index);
} else {
newFileName = fileFileName[i]+"("+time+")";
}
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
// 读取保存在临时目录下的上传文件,写入到新的文件中
try {
FileInputStream fis = new FileInputStream(file[i]);
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(new File(dir, newFileName));
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return SUCCESS;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询