1.web端使用uploadify插件 下载地址:http://www.uploadify.com/wp-content/uploads/files/uploadify.zip
web页面代码:
导入部分:
<script type="text/javascript" src="plugin/uploadify/jquery.uploadify-3.1.js"></script> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <link href="plugin/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
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); } }); }
<div style="width: 700px; height: auto; overflow-x: hidden;clear: both;" > <div style="overflow: hidden; float: left;width:200px;"><input type="file" name="files" id="file_upload" style="border: 0px;background-color: #fff;" /></div> <a href="javascript:void(0);" class="upload_btn" onclick="$('#file_upload').uploadify('upload', '*')"></a> </div> <div id="some_file_queue"></div>
提交时的参数
private List<File> files = new ArrayList<File>();
private List<String> filesFileName = new ArrayList<String>();
private List<String> filesContentType = new ArrayList<String>();
处理代码:
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<files.size(); 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);
}