1、先创建一个简单的web项目upload (如图1-1)
2、插件的准备
(1)、去uploadify的官网下载一个uploadify插件,然后解压新建个js文件夹放进去(这个不强求,只要路径对了就可以)
(2)、准备所需要的后端处理上传文件的jar包:commons-fileupload-1.2.1.jar
3、新建一个JSP即index.jsp +servlet即UploadServlet.java
图1-1
4、花几分钟对这些简单的配置完成后就可以看看index.jsp与UoloadServlet.java
(1)index.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting pageThis is my JSP page.
name="Filedata" /> |
$(document).ready(function(){
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader': 'js/uploadify/uploadify.swf',
'script': 'servlet/UploadServlet',
'cancelImg': 'js/uploadify/cancel.png',
'folder': 'upload',
'queueID' : 'custom-queue',
'auto':true,
'multi':true,
'fileDataName':'Filedata',
'onCancel' : function(file) {
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert(456);
}
});
});
});
(2).UoloadServlet.java
packagecoms;importjava.io.File;importjava.io.IOException;importjava.util.Iterator;importjava.util.List;importjava.util.UUID;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.fileupload.FileItem;importorg.apache.commons.fileupload.FileUploadException;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.fileupload.servlet.ServletFileUpload;public class UploadServlet extendsHttpServlet {
@Overrideprotected voiddoPost(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
String savePath= this.getServletConfig().getServletContext()
.getRealPath("");//保存文件的路径
savePath = savePath + "/upload/";
File f1= newFile(savePath);
System.out.println(savePath);//如果文件不存在,就新建一个
if (!f1.exists()) {
f1.mkdirs();
}//这个是文件上传需要的类,具体去百度看看,现在只管使用就好
DiskFileItemFactory fac = newDiskFileItemFactory();
ServletFileUpload upload= newServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList= null;try{
fileList=upload.parseRequest(request);
}catch(FileUploadException ex) {return;
}//迭代器,搜索前端发送过来的文件
Iterator it =fileList.iterator();
String name= "";
String extName= "";while(it.hasNext()) {
FileItem item=it.next();//判断该表单项是否是普通类型
if (!item.isFormField()) {
name=item.getName();long size =item.getSize();
String type=item.getContentType();
System.out.println(size+ " " +type);if (name == null || name.trim().equals("")) {continue;
}//扩展名格式: extName就是文件的后缀,例如 .txt
if (name.lastIndexOf(".") >= 0) {
extName= name.substring(name.lastIndexOf("."));
}
File file= null;do{//生成文件名:
name =UUID.randomUUID().toString();
file= new File(savePath + name +extName);
}while(file.exists());
File saveFile= new File(savePath + name +extName);try{
item.write(saveFile);
}catch(Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name+extName);
}
}
对于jsp与java没有好好说明,如果不懂可以私聊我,其实花了很多个小时才使用这个uploadify,主要是因为以为上传只要uploadify插件就可以,原来还需要:commons-fileupload-1.2.1.jar ,我是一个喜欢分享的人,希望每个人有能力都能写写博客,让更多人好好学习,觉得不错就点一下推荐,让更多人快速掌握。
5.鼓励:觉得写得有帮助就支付宝扫一下吧,对你没有损失,也给我动力