Java【代码】使用jspSmartUpload实现文件上传demo
2014-08-03 00:33
1046 查看
index.jsp:
servlet:
<%@page pageEncoding="utf-8"%> <h1>利用smartUpload实现文件上传</h1><hr> <center> <form method="POST" action="myuploader" ENCTYPE="multipart/form-data" > 名 称: <input type="text" name="name"><p> 缩略图片: <input type="file" name="file1" ><p> 标准图片: <input type="file" name="file2" ><p> <input type=submit value="提 交"> <input type=reset value="重 置"> </form> </center> <% if(request.getAttribute("msg")!=null) out.println("<script>alert(' "+request.getAttribute("msg")+" ' )</script>"); %>
servlet:
package com.zuidaima.smartupload.demo; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.text.SimpleDateFormat; import java.util.*; import com.jspsmart.upload.*; public class MyUploader extends HttpServlet { ServletConfig config; public void init(ServletConfig config) throws ServletException { super.init(config);//初始化servlet,主要目的是得到初始化信息 //将得到的config保存为成员变量,目的是下一步使用 this.config = config; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String msg=""; SmartUpload uploader = new SmartUpload(); try { uploader.initialize(config, request, response);// 初始化上载器 uploader.upload(); // 上载表单数据 // 此时才能读取表单数据 Enumeration<?> e = uploader.getRequest().getParameterNames(); while (e.hasMoreElements()) { //遍历所有表单参数(不包括文件) String key = (String) e.nextElement(); if ("name".equals(key)) { //找到需要的参数 //不能用request.getParameter(),只能用下列语句获取参数值 String name = uploader.getRequest().getParameterValues(key)[0]; } } for (int i = 0; i < uploader.getFiles().getCount(); i++) { com.jspsmart.upload.File myFile = uploader.getFiles().getFile(i); if (!myFile.isMissing()) { //文件上传成功 String fileName = "/upload/"+ new SimpleDateFormat("yyyyMMdd").format(new Date())+ (int) (Math.random() * 90+10)+"."+myFile.getFileExt(); myFile.saveAs(fileName, uploader.SAVE_VIRTUAL); } //上一行为提示信息 } msg="上传成功,共上传"+uploader.getFiles().getCount()+"个文件."; } catch (SmartUploadException e) { msg=e.getMessage(); //将出错信息以提示信息形式显示 e.printStackTrace(); } request.setAttribute("msg",msg); //存储提示信息 request.getRequestDispatcher("index.jsp").forward(request, response); return; }
相关文章推荐
- [JAVA]彻底抛弃 jspSmartUpload,使用 common-fileupload 实现文件上传
- 使用jspSmartUpload实现文件的上传
- jsp使用jspSmartUpload实现多文件上传
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(三):jspsmartupload samples
- 今天使用jspsmartupload.jar实现上传文件的功能,发现中文乱码,于是总结了下解决方法
- java基于servlet使用组件smartUpload实现文件上传
- 使用smartupload组件实现jsp+jdbc上传下载文件实例解析
- 理论---jsp使用jspsmartupload组件实现文件上传功能
- 使用smartupload组件实现jsp+jdbc上传下载文件
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 使用jspsmartupload上传文件抛出的java.lang.IllegalStateException异常
- java使用smartupload组件实现文件上传的方法
- java使用smartupload组件实现文件上传的方法
- JSP使用Common FileUpload组件实现文件上传及限制上传类型实例代码
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- JSP+Servlet使用jspsmartupload实现文件上传至服务器
- 使用JspSmartUpload组件上传文件
- 使用jspSmartUpload上传文件