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

Java實現(xiàn)文件上傳至服務器的方法

 更新時間:2021年07月04日 11:04:31   作者:cj634118150  
這篇文章主要為大家詳細介紹了Java實現(xiàn)文件上傳至服務器的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

但是更好的方式是使用一些已有的組件幫助我們實現(xiàn)這種上傳功能。

常用的上傳組件:  

Apache 的 Commons FileUpload

JavaZoom的UploadBean

jspSmartUpload

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ù)
 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());

  //上傳文件的保存路徑
  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" rel="external nofollow" >
 -->

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

web.xml

<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ù)-->
 <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>

本文已被整理到了《Java上傳操作技巧匯總》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中靜態(tài)類型檢查是如何進行的實例思路詳解

    Java中靜態(tài)類型檢查是如何進行的實例思路詳解

    這篇文章主要介紹了Java中靜態(tài)類型檢查是如何進行的實例思路詳解的相關資料,需要的朋友可以參考下
    2016-05-05
  • 一文看懂Mybatis中的延遲加載

    一文看懂Mybatis中的延遲加載

    這篇文章主要介紹了一文看懂Mybatis中的延遲加載,延遲加載也稱為懶加載,是指在進行表的關聯(lián)查詢時,按照設置延遲規(guī)則推遲對關聯(lián)對象的select查詢,MyBatis 的延遲加載只是對關聯(lián)對象的查詢有遲延設置,對于主加載對象都是直接執(zhí)行查詢語句的,需要的朋友可以參考下
    2023-10-10
  • 詳解Mybatis中的PooledDataSource

    詳解Mybatis中的PooledDataSource

    這篇文章主要介紹了詳解Mybatis中的PooledDataSource,PooledDataSource使用了數(shù)據(jù)庫連接池可以實現(xiàn)數(shù)據(jù)庫連接池的重復利用,還能控制連接數(shù)據(jù)庫的連接上限
    2022-06-06
  • SpringBoot+MinIO+KKFileView實現(xiàn)文件預覽功能

    SpringBoot+MinIO+KKFileView實現(xiàn)文件預覽功能

    本文主要介紹了使用SpringBoot、MinIO和KKFileView實現(xiàn)文件上傳和在線預覽功能,通過配置MinIO存儲文件,并使用KKFileView生成預覽鏈接,感興趣的可以了解一下
    2024-11-11
  • Spring jpa和mybatis整合遇到的問題解析

    Spring jpa和mybatis整合遇到的問題解析

    有朋友說jpa相比mybatis太難用,多表聯(lián)合的查詢寫起來也比較費勁,所以便加入了mybatis的支持,在配置jpa時遇到各種問題,需要修改相關配置文件,下面小編給大家分享下修改配置文件的思路,感興趣的朋友參考下
    2016-10-10
  • Java鍵盤錄入Scanner類的使用方法詳析

    Java鍵盤錄入Scanner類的使用方法詳析

    在Java編程中,引用數(shù)據(jù)類型是用來存儲對象的引用(地址),而Scanner類是引用數(shù)據(jù)類型的一種,用于讀取輸入數(shù)據(jù),文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • Java項目實現(xiàn)定時任務的三種方法

    Java項目實現(xiàn)定時任務的三種方法

    Java開發(fā)過程中經(jīng)常會遇到使用定時任務的情況,比如在某個活動結束時,自動生成獲獎名單,導出excel等,下面這篇文章主要給大家介紹了關于Java項目實現(xiàn)定時任務的三種方法,需要的朋友可以參考下
    2022-06-06
  • Java開發(fā)到底為什么要用 IoC 和 AOP

    Java開發(fā)到底為什么要用 IoC 和 AOP

    這篇文章主要介紹了Java開發(fā)到底為什么要用 IoC 和 AOP,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Java Red5服務器實現(xiàn)流媒體視頻播放

    Java Red5服務器實現(xiàn)流媒體視頻播放

    這篇文章主要介紹了Java Red5服務器實現(xiàn)流媒體視頻播放,對視頻播放感興趣的同學,可以參考下
    2021-04-04
  • springboot如何通過controller層實現(xiàn)頁面切換

    springboot如何通過controller層實現(xiàn)頁面切換

    在Spring Boot中,通過Controller層實現(xiàn)頁面切換背景,Spring Boot的默認注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會將返回值轉換為字符串返回,因此無法實現(xiàn)頁面切換,將@RestController換成@Controller
    2024-12-12

最新評論

乐昌市| 新巴尔虎右旗| 从化市| 睢宁县| 阳新县| 江津市| 海南省| 邻水| 炉霍县| 富顺县| 夹江县| 平谷区| 托克逊县| 应城市| 沙坪坝区| 正宁县| 固阳县| 涟水县| 年辖:市辖区| 灵璧县| 南平市| 信丰县| 常州市| 阿巴嘎旗| 浑源县| 宁陕县| 钦州市| 博罗县| 遂昌县| 定西市| 永安市| 清原| 边坝县| 绩溪县| 长春市| 阿勒泰市| 水城县| 肥西县| 永福县| 巴林左旗| 古蔺县|