欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java组件commons-fileupload实现文件上传、下载、在线打开

程序员文章站 2024-03-12 23:26:02
最近做了一个文件上传、下载、与在线打开文件的功能,刚开始对文件上传的界面中含有其它表单(例如输入框、密码等)在上传的过程中遇到了许多问题,下面我写了一个同时实现文件上传、下...

最近做了一个文件上传、下载、与在线打开文件的功能,刚开始对文件上传的界面中含有其它表单(例如输入框、密码等)在上传的过程中遇到了许多问题,下面我写了一个同时实现文件上传、下载、在线打开文件的测试程序。

首先请看效果图:

java组件commons-fileupload实现文件上传、下载、在线打开 

核心代码:

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

<%@ 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

<%@ 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上传操作技巧汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。