程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java組件commons-fileupload完成文件上傳、下載、在線翻開

java組件commons-fileupload完成文件上傳、下載、在線翻開

編輯:關於JAVA

java組件commons-fileupload完成文件上傳、下載、在線翻開。本站提示廣大學習愛好者:(java組件commons-fileupload完成文件上傳、下載、在線翻開)文章只能為提供參考,不一定能成為您想要的結果。以下是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上傳操作技能匯總》,迎接年夜家進修浏覽。

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved