package com.Servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.SmartUpLoad.SmartFile; import com.SmartUpLoad.SmartFiles; import com.SmartUpLoad.SmartUpload; //import com.jspsmart.upload.SmartUpload; public class ServletUpload extends HttpServlet { private ServletConfig config; /** * Init the servlet */ final public void init(ServletConfig config) throws ServletException { this.config = config; } final public ServletConfig getServletConfig() { return config; } /** * Handles GET requests */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY BGCOLOR='white'>"); out.println("<H1>jspSmartUpload : Servlet Sample</H1>"); out.println("<HR><BR>"); out.println("The method of the HTML form must be POST."); out.println("</BODY>"); out.println("</HTML>"); } /** * Handles POST requests */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Variables int count = 0; SmartUpload mySmartUpload = new SmartUpload(); try { // Initialization mySmartUpload.initialize(config, request, response); // Upload mySmartUpload.upload(); mySmartUpload.save("F:/temp/"); //写数据库 第一个文件的路径 写到“待审核软件表”的磁盘地址 第二个文件路径 写到表的图片字段 /* * SmartFiles files = mySmartUpload.getFiles(); SmartFile file = files.getFile(0); String apkName = file.getFileName();// 上传文件的名字apkName 路径+文件名写入数据库 file = files.getFile(1); String picName = file.getFileName();//上传图片的名字picName 路径+文件名写入数据库 // Display the result * * */ } catch (Exception e) { System.out.println("Unable to upload the file.<br>"); System.out.println("Error : " + e.toString()); } } }
当然 要下载SmartUpload组件 然后在servlet上import它啦