<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>文件上传</title> </head> <body> <h3>文件上传</h3> <form action=" FileSmartUpload" method="post" enctype="multipart/form-data"> 文件<input type="file" name="file1"> <input type="submit" value="提交"> </form> </body> </html> |
package com.jsp.file.upload.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; 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; @WebServlet("/FileSmartUpload") public class FileSmartUpload extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // 创建文件上传对象 SmartUpload smartUpload = new SmartUpload(); // 初始化文件上传对象 smartUpload.initialize(this.getServletConfig(),request, response); // 设置单个文件最大的字节数 smartUpload.setMaxFileSize(1000*100); // 设置总文件的最大字节数 smartUpload.setTotalMaxFileSize(1000*1000*100); // 设置允许的文件的扩展名 smartUpload.setAllowedFilesList("jpg,doc,rar,txt,java"); try { // 设置不允许的文件的扩展名 smartUpload.setDeniedFilesList("exe,php"); // 上传文件 smartUpload.upload(); // 保存文件,指定路径即可 smartUpload.save("d://"); } catch (SQLException e) { e.printStackTrace(); } catch (SmartUploadException e) { e.printStackTrace(); } } } |
原文地址:http://blog.csdn.net/u022812849/article/details/44944509