Recently, I have done a file upload, download, and open files online. At the beginning, the file upload interface contains other forms (such as input boxes, passwords, etc.) and encountered many problems during the upload process. Below I wrote a test program that simultaneously implements file upload, download, and open files online.
First, please see the renderings:
Core code:
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()) { // File 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 { // Form 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
<%@ 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> File 1: <input type="file" name="file1" /> <br> File 2: <input type="file" name="file2" /> <br> Uploader:<input type="text" name="uploader"/> <br> Date:<input type="text" name="date"/> <br> <input type="submit" value="submit"/> </form> </body> </html>
download.jsp
<%@ 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="Open:<%=fileName %>" /> </form> <% } %> </br> </body> </html>This article has been compiled into "Summary of Java Upload Operation Techniques", and everyone is welcome to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.