java组件commons-fileupload实现文件上传、下载、在线打开
更新时间:2016年10月14日 15:39:59 作者:jefry_xdz
这篇文章主要介绍了java组件commons-fileupload实现文件上传、下载、在线打开,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近做了一个文件上传、下载、与在线打开文件的功能,刚开始对文件上传的界面中含有其它表单(例如输入框、密码等)在上传的过程中遇到了许多问题,下面我写了一个同时实现文件上传、下载、在线打开文件的测试程序。
首先请看效果图:
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package com.jefry; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class FileUpload */ public class FileUpload extends HttpServlet { private static final long serialVersionUID = 1L; private static final String fileDir = "F:/" ; /** * Default constructor. */ public FileUpload() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String aFileName = request.getParameter( "fileName" ); String online = request.getParameter( "online" ); FileInputStream in = null ; ServletOutputStream out = null ; boolean isOnLine = online != null ? true : false ; try { if (isOnLine){ URL u = new URL( "file:///" +fileDir + aFileName); response.setContentType(u.openConnection().getContentType()); response.setHeader( "Content-Disposition" , "inline; filename=" +aFileName); } else { response.setContentType( "application/x-msdownload" ); response.setHeader( "Content-Disposition" , "attachment; filename=" + aFileName); } in = new FileInputStream(fileDir + aFileName); out = response.getOutputStream(); out.flush(); int aRead = 0 ; while ((aRead = in.read()) != - 1 & in != null ) { out.write(aRead); } out.flush(); } catch (Throwable e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DiskFileItemFactory fileFactory = new DiskFileItemFactory(); ServletFileUpload fu = new ServletFileUpload(fileFactory); List fileItems = fu.parseRequest(request); Iterator iter = fileItems.iterator(); String uploader = null ; String date = null ; List<String> fileNames = new ArrayList<String>(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { // 文件 String oldFileName = item.getName(); String newFileName = null ; int delimiter = oldFileName.lastIndexOf( "/" ); if (delimiter == - 1 ) newFileName = oldFileName.substring(delimiter + 1 ); else newFileName = oldFileName; fileNames.add(newFileName); item.write( new File(fileDir + newFileName)); } else { // 表单 String fieldName = item.getFieldName(); if ( "uploader" .equals(fieldName)) { uploader = item.getString(); } else if ( "date" .equals(fieldName)) { date = item.getString(); } } } request.setAttribute( "fileNames" ,fileNames); request.getRequestDispatcher( "download.jsp" ).forward(request, response); } catch (Exception e) { } } } |
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > < title >Insert title here</ title > </ head > < body > < form action = "FileUpload" method = "post" enctype = "multipart/form-data" > < br > 文件一:< input type = "file" name = "file1" /> < br > 文件二:< input type = "file" name = "file2" /> < br > 上传者:< input type = "text" name = "uploader" /> < br > 日期:< input type = "text" name = "date" /> < br > < input type = "submit" value = "提交" /> </ form > </ body > </ html > |
download.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="java.util.List"%>< html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > < title >Insert title here</ title > </ head > < body > < br > --------------直接下载------------ < br > <% List< String > fileNames = (List< String > )request.getAttribute("fileNames"); for(String fileName : fileNames) { %> < form action = "FileUpload" method = "get" > < input type = "hidden" name = "fileName" value="<%=fileName %>" /> < input type = "submit" value="下载:<%=fileName %>" /> </ form > <% } %> < br > --------------直接打开--------- <% for(String fileName : fileNames) { %> < form action = "FileUpload" method = "get" > < input type = "hidden" name = "fileName" value="<%=fileName %>" /> < input type = "hidden" name = "online" value = "yes" /> < input type = "submit" value="打开:<%=fileName %>" /> </ form > <% } %> </ br > </ body > </ html > |
本文已被整理到了《Java上传操作技巧汇总》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- java组件SmartUpload和FileUpload实现文件上传功能
- Java中使用fileupload组件实现文件上传功能的实例代码
- java使用common-fileupload实现文件上传
- Java组件commons fileupload实现文件上传功能
- JavaEE组件commons-fileupload实现文件上传、下载
- java组件commons-fileupload文件上传示例
- java组件fileupload文件上传demo
- java组件commons-fileupload实现文件上传
- JAVA使用commos-fileupload实现文件上传与下载实例解析
- 使用fileupload组件实现文件上传功能
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
相关文章
SpringCloud Gateway详细分析实现负载均衡与熔断和限流
这篇文章主要介绍了SpringCloud Gateway实现路由转发,负载均衡,熔断和限流,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-07-07Spring Boot 整合RocketMq实现消息过滤功能
这篇文章主要介绍了Spring Boot 整合RocketMq实现消息过滤,本文讲解了RocketMQ实现消息过滤,针对不同的业务场景选择合适的方案即可,需要的朋友可以参考下2022-06-06使用IDEA搭建Hadoop开发环境的操作步骤(Window10为例)
经过三次重装,查阅无数资料后成功完成hadoop在win10上实现伪分布式集群,以及IDEA开发环境的搭建。一步一步跟着本文操作可以避免无数天坑2021-07-07
最新评论