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

java組件fileupload文件上傳demo

編輯:關於JAVA

java組件fileupload文件上傳demo。本站提示廣大學習愛好者:(java組件fileupload文件上傳demo)文章只能為提供參考,不一定能成為您想要的結果。以下是java組件fileupload文件上傳demo正文


在我們的web開辟中,許多的時刻都須要把本機的一些文件上傳到web辦事器下面去。

如:一個BBS體系,當用戶應用這是體系的時刻,能把本機的一些圖片,文檔上傳到辦事器下面去。然後其他用戶可以去下載這些文件,那末如許的話,我們可以本身編程完成文件的上傳,然則更好的方法是應用一些已有的組件贊助我們完成這類上傳功效。

經常使用的上傳組件:  

    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中設置的一個初始化參數
 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("表單參數名:" + item.getFieldName() + ",表單參數值:" + 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());

  //上傳文件的保留途徑
  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>

  <!--設置初始化參數-->
 <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>

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

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