欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java組件fileupload文件上傳demo

 更新時間:2016年10月14日 11:53:50   作者:壹龍  
這篇文章主要為大家詳細(xì)介紹了java組件fileupload文件上傳demo ,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在我們的web開發(fā)中,很多的時候都需要把本機的一些文件上傳到web服務(wù)器上面去。

如:一個BBS系統(tǒng),當(dāng)用戶使用這是系統(tǒng)的時候,能把本機的一些圖片,文檔上傳到服務(wù)器上面去。然后其他用戶可以去下載這些文件,那么這樣的話,我們可以自己編程實現(xiàn)文件的上傳,但是更好的方式是使用一些已有的組件幫助我們實現(xiàn)這種上傳功能。

常用的上傳組件:  

    Apache 的 Commons FileUpload

    JavaZoom的UploadBean

    jspSmartUpload

FileUpload下載地址

  http://commons.apache.org/fileupload/

  下載:commons-fileupload-1.2.2-bin.zip    得到:commons-fileupload-1.2.2.jar

  http://commons.apache.org/io/

  下載:commons-io-1.4-bin.zip       得到:commons-io-1.4.jar

upload.jsp

代碼;

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>using commons Upload to upload file </title>
</head>
<style>
* { font-family: "宋體"; font-size: 14px }
</style>
<body>
<p align="center"> 請您選擇需要上傳的文件</p>
<form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data">
 <table border="0" align="center">
 <tr>
 <td>上傳人:</td>
 <td>
 <input name="name" type="text" id="name" size="20" ></td>
 </tr> 
 <tr>
 <td>上傳文件:</td>
 <td><input name="file" type="file" size="20" ></td>
 </tr> 
 <tr> 
 <td></td><td>
 <input type="submit" name="submit" value="提交" >
 <input type="reset" name="reset" value="重置" >
 </td>
 </tr>
 </table>
</form>
</body>
</html>

FileUploadServlet.java代碼:

package com.b510.example;

import java.io.File;
import java.io.IOException;
import java.util.*;


import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * 
 * @author XHW
 * 
 * @date 2011-7-26
 * 
 */
public class FileUploadServlet extends HttpServlet {

 private static final long serialVersionUID = -7744625344830285257L;
 private ServletContext sc;
 private String savePath;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 doPost(request, response);
 }
 

 public void init(ServletConfig config) {
 // 在web.xml中設(shè)置的一個初始化參數(shù)
 savePath = config.getInitParameter("savePath");
 sc = config.getServletContext();
 }
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("UTF-8");
 DiskFileItemFactory factory = new DiskFileItemFactory();
 ServletFileUpload upload = new ServletFileUpload(factory);
 try {
 List items = upload.parseRequest(request);
 Iterator itr = items.iterator();
 while (itr.hasNext()) {
 FileItem item = (FileItem) itr.next();
 if (item.isFormField()) {
  System.out.println("表單參數(shù)名:" + item.getFieldName() + ",表單參數(shù)值:" + item.getString("UTF-8"));
 } else {
  if (item.getName() != null && !item.getName().equals("")) {
  System.out.println("上傳文件的大小:" + item.getSize());
  System.out.println("上傳文件的類型:" + item.getContentType());
  // item.getName()返回上傳文件在客戶端的完整路徑名稱
  System.out.println("上傳文件的名稱:" + item.getName());

  File tempFile = new File(item.getName());

 ?。蟼魑募谋4媛窂?
  File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
  item.write(file);
  request.setAttribute("upload.message", "上傳文件成功!");
  }else{
  request.setAttribute("upload.message", "沒有選擇上傳文件!");
  }
 }
 }
 }catch(FileUploadException e){
 e.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
 request.setAttribute("upload.message", "上傳文件失??!");
 }
 request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
 }
}

uploadResult.jsp代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 
 <title>uploadResult</title>
 
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>
 
 <body>
 ${requestScope['upload.message'] }
 <a href="/upload/uploadFile.jsp">上傳文件</a>
 </body>
</html>

web.xml

代碼:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>FileUploadServlet</servlet-name>
 <servlet-class>com.b510.example.FileUploadServlet</servlet-class>

 ?。?!--設(shè)置初始化參數(shù)-->
 <init-param>
  <param-name>savePath</param-name>
  <param-value>uploads</param-value>
 </init-param>
 </servlet>

 <servlet-mapping>
 <servlet-name>FileUploadServlet</servlet-name>
 <url-pattern>/servlet/fileServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>uploadFile.jsp</welcome-file>
 </welcome-file-list>
</web-app>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實現(xiàn)文件導(dǎo)入導(dǎo)出

    java實現(xiàn)文件導(dǎo)入導(dǎo)出

    這篇文章主要介紹了java實現(xiàn)文件導(dǎo)入導(dǎo)出的方法和具體示例代碼,非常的簡單實用,有需要的小伙伴可以參考下
    2016-04-04
  • Maven如何修改打包文件名稱

    Maven如何修改打包文件名稱

    這篇文章主要介紹了Maven如何修改打包文件名稱問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java?stream流中peek用法簡單示例

    Java?stream流中peek用法簡單示例

    這篇文章主要給大家介紹了關(guān)于Java?stream流中peek用法的相關(guān)資料,Java Stream中的peek()方法也是用于查看每個元素,但不改變流的操作的方法,文中通過代碼介紹的需要的朋友可以參考下
    2023-12-12
  • 淺談Java線程并發(fā)知識點

    淺談Java線程并發(fā)知識點

    本文主要對Java線程并發(fā)的知識點進行簡單介紹。具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • Java判斷字符串為空、字符串是否為數(shù)字

    Java判斷字符串為空、字符串是否為數(shù)字

    這篇文章主要介紹了Java判斷字符串為空、字符串是否為數(shù)字,其中數(shù)字的判斷介紹了3種方法,需要的朋友可以參考下
    2014-06-06
  • Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之哈希算法實現(xiàn)

    Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之哈希算法實現(xiàn)

    哈希表本質(zhì)是一種(key,value)結(jié)構(gòu),由此我們可以聯(lián)想到,能不能把哈希表的key映射成數(shù)組的索引index呢?如果這樣做的話那么查詢相當(dāng)于直接查詢索引,查詢時間復(fù)雜度為O(1),其實這也正是當(dāng)key為int型時的做法,將key通過某種做法映射成index,從而轉(zhuǎn)換成數(shù)組結(jié)構(gòu)
    2022-02-02
  • java nio基礎(chǔ)使用示例

    java nio基礎(chǔ)使用示例

    傳統(tǒng)的io技術(shù)為阻塞的,java新nio是非阻塞的,注冊一個op_read事件,注冊到selector對象上,當(dāng)有數(shù)據(jù)到來時候,selector回通知之前注冊事件的對象,進行read處理,看面我看看它是如何使用的
    2013-11-11
  • GC參考手冊jvm垃圾回收詳解

    GC參考手冊jvm垃圾回收詳解

    顧名思義,垃圾收集(Garbage?Collection)的意思就是?——?找到垃圾并進行清理。但現(xiàn)有的垃圾收集實現(xiàn)卻恰恰相反:?垃圾收集器跟蹤所有正在使用的對象,并把其余部分當(dāng)做垃圾。記住這一點以后,?我們再深入講解內(nèi)存自動回收的原理,探究?JVM?中垃圾收集的具體實現(xiàn)
    2022-01-01
  • Java中的CompletionService批量異步執(zhí)行詳解

    Java中的CompletionService批量異步執(zhí)行詳解

    這篇文章主要介紹了Java中的CompletionService批量異步執(zhí)行詳解,我們知道線程池可以執(zhí)行異步任務(wù),同時可以通過返回值Future獲取返回值,所以異步任務(wù)大多數(shù)采用ThreadPoolExecutor+Future,需要的朋友可以參考下
    2023-12-12
  • java多線程關(guān)鍵字final和static詳解

    java多線程關(guān)鍵字final和static詳解

    這篇文章主要介紹了java多線程關(guān)鍵字final和static詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01

最新評論