最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java組件fileupload文件上傳demo

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

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

如:一個(gè)BBS系統(tǒng),當(dāng)用戶使用這是系統(tǒng)的時(shí)候,能把本機(jī)的一些圖片,文檔上傳到服務(wù)器上面去。然后其他用戶可以去下載這些文件,那么這樣的話,我們可以自己編程實(shí)現(xiàn)文件的上傳,但是更好的方式是使用一些已有的組件幫助我們實(shí)現(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"> 請(qǐng)您選擇需要上傳的文件</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è)置的一個(gè)初始化參數(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>

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

相關(guān)文章

  • SpringBoot限制接口訪問頻率功能實(shí)現(xiàn)

    SpringBoot限制接口訪問頻率功能實(shí)現(xiàn)

    最近在基于SpringBoot做一個(gè)面向普通用戶的系統(tǒng),為了保證系統(tǒng)的穩(wěn)定性,防止被惡意攻擊,我想控制用戶訪問每個(gè)接口的頻率,接下來通過本文給大家介紹SpringBoot限制接口訪問頻率功能實(shí)現(xiàn),需要的朋友可以參考下
    2023-05-05
  • Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法總結(jié)

    Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法總結(jié)

    在本篇文章里我們給大家分享了關(guān)于Java中Aspose組件進(jìn)行多文檔間的轉(zhuǎn)換方法內(nèi)容,需要的朋友們學(xué)習(xí)下吧。
    2019-02-02
  • Java中的數(shù)組使用詳解及練習(xí)

    Java中的數(shù)組使用詳解及練習(xí)

    數(shù)組是Java程序中最常見的一種數(shù)據(jù)結(jié)構(gòu),它能夠?qū)⑾嗤愋偷臄?shù)據(jù)用一個(gè)標(biāo)識(shí)符封裝到一起,構(gòu)成一個(gè)對(duì)象序列或基本數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于Java中數(shù)組使用詳解及練習(xí)的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Java之字節(jié)碼以及優(yōu)勢(shì)案例講解

    Java之字節(jié)碼以及優(yōu)勢(shì)案例講解

    這篇文章主要介紹了Java之字節(jié)碼以及優(yōu)勢(shì)案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 解讀synchronized鎖的釋放機(jī)制

    解讀synchronized鎖的釋放機(jī)制

    這篇文章主要介紹了synchronized鎖的釋放機(jī)制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list

    Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list

    這篇文章主要介紹了Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中的ArrayList(擴(kuò)容機(jī)制)詳解

    Java中的ArrayList(擴(kuò)容機(jī)制)詳解

    ArrayList作為Java中廣泛使用的動(dòng)態(tài)數(shù)組,其擴(kuò)容機(jī)制是保證性能和內(nèi)存使用平衡的關(guān)鍵,默認(rèn)初始容量為10,擴(kuò)容因子為1.5,旨在減少頻繁的內(nèi)存分配和數(shù)據(jù)遷移代價(jià),同時(shí)建議使用預(yù)估計(jì)的初始化容量以減少擴(kuò)容次數(shù)
    2024-11-11
  • Java實(shí)現(xiàn)文件名倒序排序的技術(shù)指南

    Java實(shí)現(xiàn)文件名倒序排序的技術(shù)指南

    在實(shí)際開發(fā)過程中,我們經(jīng)常需要對(duì)文件進(jìn)行操作和處理,一個(gè)常見的需求是按文件名倒序排列文件列表,以便于文件的管理和查找,本文將介紹如何在Java中實(shí)現(xiàn)文件名倒序排序,并提供詳細(xì)的代碼案例,需要的朋友可以參考下
    2024-08-08
  • Java設(shè)計(jì)模式開發(fā)中使用觀察者模式的實(shí)例教程

    Java設(shè)計(jì)模式開發(fā)中使用觀察者模式的實(shí)例教程

    這篇文章主要介紹了Java設(shè)計(jì)模式開發(fā)中使用觀察者模式的實(shí)例教程,松耦合和邏輯清晰的消息監(jiān)聽是觀察者模式的大特色,需要的朋友可以參考下
    2016-04-04
  • java synchronized關(guān)鍵字的用法

    java synchronized關(guān)鍵字的用法

    synchronized關(guān)鍵字我們大家都知道是線程同步關(guān)鍵字.總結(jié)一下日常的使用方法,還有一個(gè)坑.
    2016-05-05

最新評(píng)論

高雄市| 万安县| 红原县| 逊克县| 丰顺县| 岳普湖县| 江西省| 漠河县| 灵丘县| 桂阳县| 房山区| 满城县| 昂仁县| 图们市| 莱西市| 绥化市| 武功县| 临桂县| 顺昌县| 古交市| 张家界市| 城固县| 柞水县| 广德县| 二手房| 民勤县| 孝昌县| 治多县| 洛扎县| 柳林县| 吉林省| 阿图什市| 田林县| 淮滨县| 奈曼旗| 德钦县| 邢台县| 天长市| 哈巴河县| 九龙坡区| 平定县|