標籤:smart method orm multi https count except throw ini
檔案上傳:
upload.jsp
<form action="uploadServlet" method="post" enctype="multipart/form-data"> 作者:<input type="text" name="author"/><br/> 檔案1:<input type="file" name="file"/><br/> 檔案2:<input type="file" name="file"/><br/> <input type="submit" value="上傳"/> </form>
UploadServlet:
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.File;import com.jspsmart.upload.Files;import com.jspsmart.upload.Request;import com.jspsmart.upload.SmartUpload;public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String author1 = request.getParameter("author"); System.out.println("author1="+author1); //建立smartUpload組件對象 SmartUpload smartUpload = new SmartUpload(); //初始化當前對象 smartUpload.initialize(this.getServletConfig(), request, response); //控制總檔案的上傳大小(單位B)(10MB) smartUpload.setTotalMaxFileSize(1024*1024*10); //控制單個檔案的上傳大小(單位B)(1MB) smartUpload.setMaxFileSize(1024*1024); try { //上傳檔案 smartUpload.upload(); //擷取smartUpload組件裡的request對象 Request req = smartUpload.getRequest(); String author = req.getParameter("author"); System.out.println("author="+author); //擷取上傳檔案 Files files = smartUpload.getFiles(); //擷取單個檔案 for(int i=0;i<files.getCount();i++){ File file = files.getFile(i); file.saveAs("d:\\upload\\"+file.getFileName()); } } catch (Exception e) { e.printStackTrace(); } }}
檔案下載:
download.jsp
<img src="img/wa.png"><a href="downLoadServlet?fileName=wawa.jpg">下載</a>
DownloadServlet:
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //擷取下載的檔案名稱 String fileName = request.getParameter("fileName"); //建立smartUpload組件對象 SmartUpload smartUpload = new SmartUpload(); //初始化當前對象 smartUpload.initialize(this.getServletConfig(), request, response); //禁止瀏覽器自動開啟下載的檔案 smartUpload.setContentDisposition(null); try { smartUpload.downloadFile("download/"+fileName); } catch (Exception e) { e.printStackTrace(); } }}
jsp實現檔案上傳下載