使用common-fileUpload实现文件上传
来源:互联网 发布:网络路径错误怎么办 编辑:程序博客网 时间:2024/04/30 21:47
通过common-fileUpload文件上传组件,可以实现上传文档、图片等资料。根据程序的不同要求,它可以有多种方式的应用。
我们这里介绍一种简单的例子,来实现文件上传功能。
准备:
引入相关组建的jar包
首先,我们进入上传页面index.html.
<body><form action="./servlet/item/FileUploadServlet" method="post"enctype="multipart/form-data" name="form1"><input type="file" name="file"><input type="submit" name="Submit" value="upload"></form><form action="./servlet/HelloWord" method="post"><input type="submit" /></form><form name="uploadform" method="POST" action="./servlet/item/FileUploadServlet"ENCTYPE="multipart/form-data"><table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF"><tr><td width="100%" colspan="2">文件<input name="x" size="40" type="file"></td></tr></table><table><tr><td align="center"><input name="upload" type="submit" value="开始上传" /></td></tr></table></form></body>
选择要上传的文件,点击开始上传,跳转到servlet页面,即下面提到的FileUploadServlet。
/** * 实现文件上传的Servlet * @author Administrator * */public class FileUploadServlet extends HttpServlet {//上传路径private File uploadPath;//当文件过大时,需要设置一个临时路径private File tempPath;public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {DiskFileItemFactory factory = new DiskFileItemFactory();// 内存存储的最大值factory.setSizeThreshold(4096);factory.setRepository(tempPath);ServletFileUpload upload = new ServletFileUpload(factory);//设置文件上传大小upload.setSizeMax(1000000 * 20);try {List fileItems = upload.parseRequest(req);String itemNo = "";for (Iterator iter = fileItems.iterator(); iter.hasNext();) {FileItem item = (FileItem) iter.next();//是普通的表单输入域if(item.isFormField()) {if ("itemNo".equals(item.getFieldName())) {itemNo = item.getString();}}//是否为input="type"输入域if (!item.isFormField()) {String fileName = item.getName();long size = item.getSize();if ((fileName == null || fileName.equals("")) && size == 0) {continue;}//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOGfileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());//item.write(new File(uploadPath + itemNo + ".gif"));item.write(new File(uploadPath, itemNo + ".gif"));}}//重定向页面res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");} catch (Exception e) {e.printStackTrace();}}/** * 初始化方法,设定目录 */public void init() throws ServletException {uploadPath = new File(getServletContext().getRealPath("upload"));//System.out.println("uploadPath=====" + uploadPath);//如果目录不存在if (!uploadPath.exists()) {//创建目录uploadPath.mkdir();}//临时目录tempPath = new File(getServletContext().getRealPath("temp"));if (!tempPath.exists()) {tempPath.mkdir();}}}小结:
FileUpload上传一个文件的过程中可以分为三个部分:首先,客户端与服务器端建立连接,把要上传的文件生成request数据流。其次服务器端接收request流,将流缓存到内存或磁盘中。当文件过大时,文件将被缓存到内存,否则将被缓存到磁盘的临时文件。最后由服务器端的内存或是临时文件中把文件输出到指定的目录,这个目录才是指定的文件上传目录。
0 2
- 使用 common-fileupload 实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileupload框架实现文件上传
- 使用common-fileupload实现文件上传功能
- 使用common-fileupload实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileUpload实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileupload实现文件的上传
- Struts2中使用Common-FileUpload实现文件上传
- Struts2下使用Common-FileUpload实现文件上传
- Struts2中使用Common-FileUpload实现文件上传(二)
- Struts2中使用Common-FileUpload实现文件上传(二)
- Struts2下使用Common-FileUpload实现文件上传
- 使用common-fileUpload和 Spring中MultipartHttpServletRequest实现文件上传
- Common-FileUpload实现文件上传,实例一
- 基于common-fileupload实现文件上传
- 使用common-fileUpload制作文件上传
- STM32芯片内部温度传感器
- SELinux深入理解
- 张正友标定算法理论及算法实现
- vs2012停止调试长时间不响应问题解决方法
- hdu 1250Hat's Fibonacci
- 使用common-fileUpload实现文件上传
- 从nsdate中分别获取年月日的代码写法(iOS)
- mongodb3.x远程登录和认证问题
- CF 508C///贪心
- 两台Linux主机通信(服务器客户端搭建)
- Android Handler 异步消息处理机制
- 接口
- UML——概述
- Network