文件上传与下载
文件上传与下载
a.使用 ajaxfileupload.js 完成文件上传
http://www.phpletter.com/Our-Projects/AjaxFileUpload/
js 方法
1 function ajaxFileUpload() 2 { 3 $("#loading_img").ajaxStart(function(){ 4 $(this).show(); //开始上传文件时显示一个图片 5 }).ajaxComplete(function(){ 6 $(this).hide(); //文件上传完成将图片隐藏起来 7 }); 8 9 $.ajaxFileUpload 10 ( 11 { 12 url:'FileUpLoadServlet',//用于文件上传的服务器端请求地址 13 secureuri:false,//一般设置为false 14 fileElementId:'file_ajax',//文件上传空间的id属性 <input type="file" id="file" name="file" /> 15 dataType: 'json',//返回值类型 一般设置为json 16 success: function (data, status) //服务器成功响应处理函数 17 { 18 //从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量 19 alert(data.message); 20 21 if(typeof(data.error) != 'undefined') 22 { 23 if(data.error != '') 24 { 25 alert(data.error); 26 }else 27 { 28 alert(data.message); 29 } 30 } 31 }, 32 error: function (data, status, e)//服务器响应失败处理函数 33 { 34 alert(e); 35 } 36 } 37 ) 38 39 return false; 40 41 }
HTML代码
1 <img src="images/indicator.gif" id="loading_img" style="display: none;"> 2 <input type="file" id="file_ajax" name="file_ajax"> 3 <input type="button" value="文件上传" onclick="ajaxFileUpload()"/>
java 部分的代码 (来源于网络,仅作参考)
http://www.cnblogs.com/hongten/archive/2011/07/26/2117340.html
1 package ajax.fileupload; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import javax.servlet.ServletConfig; 10 import javax.servlet.ServletContext; 11 import javax.servlet.ServletException; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 import org.apache.commons.fileupload.FileItem; 17 import org.apache.commons.fileupload.FileUploadException; 18 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 19 import org.apache.commons.fileupload.servlet.ServletFileUpload; 20 21 public class FileUpLoadServlet extends HttpServlet { 22 23 private static final long serialVersionUID = -7744625344830285257L; 24 private ServletContext sc; 25 private String savePath; 26 27 28 public FileUpLoadServlet() { 29 super(); 30 } 31 32 public void doGet(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 35 doPost(request, response); 36 } 37 38 public void init(ServletConfig config) throws ServletException 39 { 40 // 在web.xml中设置的一个初始化参数 41 savePath = config.getInitParameter("savePath"); 42 sc = config.getServletContext(); 43 } 44 45 public void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 48 request.setCharacterEncoding("UTF-8"); 49 response.setCharacterEncoding("UTF-8"); 50 DiskFileItemFactory factory = new DiskFileItemFactory(); 51 ServletFileUpload upload = new ServletFileUpload(factory); 52 try 53 { 54 List items = upload.parseRequest(request); 55 Iterator itr = items.iterator(); 56 57 while (itr.hasNext()) 58 { 59 FileItem item = (FileItem) itr.next(); 60 if (item.isFormField()) 61 { 62 System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8")); 63 } 64 else 65 { 66 if (item.getName() != null && !item.getName().equals("")) 67 { 68 System.out.println("上传文件的大小:" + item.getSize()); 69 System.out.println("上传文件的类型:" + item.getContentType()); 70 71 // item.getName()返回上传文件在客户端的完整路径名称 72 System.out.println("上传文件的名称:" + item.getName()); 73 74 File tempFile = new File(item.getName()); 75 76 //上传文件的保存路径 77 File file = new File(sc.getRealPath("/") + savePath, tempFile.getName()); 78 item.write(file); 79 80 PrintWriter out = response.getWriter(); 81 out.println("{'message':'上传文件成功!'}"); 82 } 83 else 84 { 85 request.setAttribute("message", "没有选择上传文件!"); 86 } 87 } 88 } 89 } 90 catch(FileUploadException e) 91 { 92 e.printStackTrace(); 93 } 94 catch (Exception e) 95 { 96 e.printStackTrace(); 97 request.setAttribute("message", "上传文件失败!"); 98 } 99 100 //request.getRequestDispatcher("/uploadResult.jsp").forward(request, response); 101 } 102 103 }
需要引入的jar
FileUpload下载地址:
http://commons.apache.org/fileupload/
下载:commons-fileupload-1.2.2-bin.zip 得到:commons-fileupload-1.2.2.jar
下载:commons-io-1.4-bin.zip 得到:commons-io-1.4.jar
web.xml文件中需要配置
1 <servlet> 2 ..... 3 4 <!--设置初始化参数--> 5 <init-param> 6 <param-name>savePath</param-name> 7 <param-value>uploads</param-value> 8 </init-param> 9 </servlet>
在tomcat的webapps目录下建 文件夹 uploads
......\webapps\UploadFile\uploads
有进度条的文件上传