首页 > 代码库 > jsp实现文件上传下载
jsp实现文件上传下载
文件上传:
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="http://www.mamicode.com/img/wa.png"><a href="http://www.mamicode.com/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实现文件上传下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。