smartupload组件上传文件
2015-06-28 00:06
148 查看
jsp部分:
如果要进行文件的上传的操作的话,则必须对表单进行封装,也就说将表单中的ENCTYPE属性设置为: ”multipart/form-data”
servlet部分:
web.xml
但是不要忘记了,一定要加上smartupload.jar 这个jar包
转自转弯梦之恋的博客
<%@ page language="java" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>smart_demo03.jsp</title> </head> <body> <h3><font color="red">文件上传的操作---SmartUpload</font></h3> <form action="<%=path %>/SmartUploadServlet" method="post" ENCTYPE="multipart/form-data"> 图片:<input type="file" name="pic"/><br> <input type="submit" value="上传"/> </form> </body> </html>
如果要进行文件的上传的操作的话,则必须对表单进行封装,也就说将表单中的ENCTYPE属性设置为: ”multipart/form-data”
servlet部分:
package com.smart; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspFactory; import javax.servlet.jsp.PageContext; import com.jspsmart.upload.File; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; /** * 图片上传操作 * * @version 2013/08/08 16:04:02 * @author */ public class SmartUploadServlet extends HttpServlet{ /** * 允许上传的图片类型 */ private static final String ALLOWEDFILESLIST = "jpg,gif,bmp"; /** * 允许图片的大小 */ private static final long MAXSIZE = 2 * 1024 * 1024; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String messages = ""; String forward = ""; // 获取pageContext PageContext pageContext = JspFactory.getDefaultFactory() .getPageContext(this, request, response, null, true, 8192, true); SmartUpload upload = new SmartUpload(); try { // 初始化 upload.initialize(pageContext); // 限制上传文件的大小 upload.setMaxFileSize(MAXSIZE); // 设置允许上传的文件类型 upload.setAllowedFilesList(ALLOWEDFILESLIST); // 准备上传 upload.upload(); // 系统时间做为文件名 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String sysDate = sdf.format(new Date()); // 保存文件 for (int i = 0; i < upload.getFiles().getCount(); i++) { File file = upload.getFiles().getFile(i); // 如果选择了文件 if (!file.isMissing()) { // 获取图片的拓展名 String ext = upload.getFiles().getFile(i).getFileExt(); // 为文件重新命名 String fileName = sysDate + "." + ext; file.saveAs("D:" + java.io.File.separator + "upload" + java.io.File.separator + fileName, file.SAVEAS_PHYSICAL); } } } catch (java.lang.SecurityException e) { messages = "上传的文件类型只允许为:" + ALLOWEDFILESLIST; forward = "/index.jsp"; } catch (SmartUploadException e) { messages = "上传文件失败!"; forward = "/index.jsp"; e.printStackTrace(); } request.setAttribute("messages", messages); request.getRequestDispatcher(forward).forward(request, response); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>SmartUploadServlet</servlet-name> <servlet-class>com.smart.SmartUploadServlet(这里写调用的servlet文件)</servlet-class> </servlet> <servlet-mapping> <servlet-name>SmartUploadServlet</servlet-name> <url-pattern>/SmartUploadServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
但是不要忘记了,一定要加上smartupload.jar 这个jar包
转自转弯梦之恋的博客
相关文章推荐
- 核心动画(Core Animation)
- 欢迎使用CSDN-markdown编辑器
- c#中使用easyUI的DataGrid组件
- 前端工程师必须要知道的SEO技巧(2):制作比设计还要漂亮的代码(内容和语义化代码)实现下
- 8.Eclipse安装SVN? 解决方法:
- ZooKeeper 笔记(3) 实战应用之【统一配置管理】
- 2015062711 - 鸟叫了就出去,鬼叫了就回来
- 静态导入
- 关于MBProgressHUD显示问题
- 7. Eclipse 报错:Error: Program "sh" not found in PATH
- 开源一个适用iOS的数据库表结构更新机制的代码
- 重装操作系统后,要重新配置Android开发环境。配置成功后,添加原本项目时却出现了错误! Android requires compiler compliance level 5.0 or 6.0.
- java过滤器——filter的使用和配置
- 中国便利店区域战:下沉、土味、差异化
- 5. android:windowSoftInputMode属性详解
- 基于日出和日落时间自动切换到明/暗 Gtk 主题
- 自动化Nginx服务器的反向代理的配置方法
- python实现linux下使用xcopy的方法
- 分析用Python脚本关闭文件操作的机制
- python实现搜索指定目录下文件及文件内搜索指定关键词的方法