1.web端使用uploadify插件 下载地址:http://www.uploadify.com/wp-content/uploads/files/uploadify.zip
web页面代码:
导入部分:
js代码部分:
$function(){
$("#file_upload").uploadify({
'auto' : false,// 关闭自动提交
'multi' : false,
'queueSizeLimit' : 1,
'fileTypeDesc' : '支持格式:zip',// 文件类型
'fileTypeExts' : '*.zip',// 文件后缀
'formData' : {'paramname1':'paramvalue1','paramname...':'paramvalue...'},// 提交时跟上去的参数
'fileObjName': 'files',
'fileSizeLimit' :'200MB',
//'removeTimeout': 1,// 从界面上移除的时间设置
"buttonImage": path+'manage/plugin/uploadify/img/add_Flie.png',
'swf': path+'manage/plugin/uploadify/uploadify.swf',
'uploader': path+'jsonmanager/doArticle.action',
'onQueueComplete' : function(queueData) {
$('#file_upload').uploadify('disable', false);
$('.upload_btn').attr('disabled', false);
},
'onUploadSuccess' : function(file, data, response) {
},
'onUploadStart': function(file) {
$('#file_upload').uploadify('disable', true);
$('.upload_btn').attr('disabled', true);
}
});
}
页面调用部分:
请求的java代码:
提交时的参数
private List files = new ArrayList();
private List filesFileName = new ArrayList();
private List filesContentType = new ArrayList();
处理代码:
try {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
Long size = Long.parseLong(request.getHeader("Content-Length"));
StringBuffer ss = new StringBuffer();//存放返回的内容
for (int i = 0; i
String type = filesContentType.get(i);
String name = filesFileName.get(i);
int index = name.lastIndexOf(".");//原名称里倒 数第一个"."在哪里
String ext = name.substring(index+1); //取得后缀,及"."后面的字符
String tempFileName = name.substring(0, index);
byte data[] = new byte[1024 * 1024]; // 用于存放流数据
File file = new File(Utils.SPECIAL_ZIP_ADDRESS,tempFileName + "." + ext); // 新建文件 Utils.SPECIAL_ZIP_ADDRESS 服务器存放地址路径
//上传的文件存在时删除之前的专题文件
OutputStream fileOutputStream = null;
InputStream in = null;
try {
fileOutputStream = new FileOutputStream(file); // 创建文件流
in = new FileInputStream(files.get(i));
int bytesRead = 0;
while ((bytesRead = in.read(data)) > 0) {
fileOutputStream.write(data, 0, bytesRead); // 写入文件流
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally{
if(fileOutputStream != null){
fileOutputStream.close();
fileOutputStream = null;
}
if(in != null){
in.close();
in = null;
}
}
}
}
response.setCharacterEncoding("utf-8");
response.getWriter().write(ss.toString());
response.getWriter().close();
} catch (Exception e) {
exceptionHandler(e);
}
分享到:
2012-10-19 17:03
浏览 2817
评论