下載地址:http://download.csdn.net/detail/heidan2006/182263
如何添加jspsmartupload.jar:
右鍵“Web”工程-》properties-》Libraries-》Add External JARs...-》找到“jspsmartupload.jar”,添加進去
-》點擊“OK”-》找到“E:\MyEclipse2014\projects\Web\WebRoot\WEB-INF\lib”路徑,把“jspsmartupload.jar”添加進去
-》重啟tomcat,不然出現invalid
uploadForm.html
<!DOCTYPE html> <html> <head> <title>uploadForm.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=gbk"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="usingsmartupload.jsp" method="post" enctype="multipart/form-data" name="theFile"> <input type="file" name="file" size="30"/><br> <input type="submit" value="上傳" name="btn"> </form> </body> </html>
usingsmartupload.jsp
<%@page import="java.io.File"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!-- java.sql.*包可以不導 --> <%@page import="java.sql.*" %> <%@page import="com.jspsmart.upload.*" %> <%@page import="java.io.*" %> <jsp:useBean id="the" scope="page" class="com.jspsmart.upload.SmartUpload"/> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'usingsmartupload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- 這段代碼可以不加 --> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% try{ the.initialize(pageContext); the.setTotalMaxFileSize(5*1024*1024); the.upload(); String fn=the.getFiles().getFile(0).getFieldName(); path=request.getRealPath("")+"\\myfile"; //java.io.File d=new java.io.File(path); File d=new File(path); if(!d.exists()){ d.mkdirs(); } the.save(path); //the.save("E:\\FFOutput\\"); out.print(fn); out.print("文件已上傳成功!!!"); } catch(Exception e){ e.printStackTrace(); } %> </body> </html>
方法二:通過跳轉Servlet上傳
public class upfile extends HttpServlet { private static final long serialVersionUID = 1L; public upfile() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); SmartUpload up=new SmartUpload(); try{ /* 第一個參數是傳遞一個Servlet,在servlet中傳遞this就可以了; 第二個和第三個參數是request與response不多說明了; 第四個參數是發生錯誤后的url路徑地址,如果沒有可以鍵入null; 第五個參數是是否需要session,這里可以寫入true; 第六個參數是緩存大小,我們用了8*1024; 第七個蠶食是是否需要刷新,鍵入ture;*/ PageContext context = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8*1024, true); up.initialize(context); up.setTotalMaxFileSize(5*1024*1024); //上傳的總文件大小不能超過這個 up.upload(); //獲取文件名 //String fn=up.getFiles().getFile(0).getFieldName(); String path="E:\\Tomcat\\webapps\\MyWeb\\myfile"; //java.io.File d=new java.io.File(path); File d=new File(path); if(!d.exists()){ d.mkdirs(); } up.save(path); //System.out.print(fn); //System.out.print("文件上傳成功啦!!!"); RequestDispatcher dispatcher=request.getRequestDispatcher("/splitpage2/downfile.jsp"); dispatcher.forward(request, response); //無法傳參 //response.sendRedirect("http://localhost:8080/MyWeb/splitpage2/list.jsp"); } catch(Exception e){ e.printStackTrace(); } } public void init() throws ServletException { // Put your code here } }