Java component Commons-fileupload Implementation file upload, download, open online _java

Source: Internet
Author: User
Tags file upload flush throwable filefactory

Recently made a file upload, download, and online Open the function of the file, just began to upload the file interface contains other forms (such as input boxes, passwords, etc.) in the upload process encountered a lot of problems, I wrote a file upload, download, open the file online test procedures.

First, look at the effect chart:

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) th Rows 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, Ioexce 
  ption {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); 
 The 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" > 
 
 

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 impo Rt= "Java.util.List"%> 

This article has been sorted to "Java Upload Operation skills Summary", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.