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

java eclipse 中文件的上傳和下載示例解析

 更新時(shí)間:2020年07月14日 17:30:52   作者:java小斌  
這篇文章主要介紹了eclipse java中文件的上傳和下載示例解析,文件上傳,瀏覽器在上傳的過(guò)程中是將文件以流的形式提交到服務(wù)器端的。對(duì)此感興趣的小伙伴可以了解一下

文件的上傳與下載(一)

在實(shí)現(xiàn)文件上傳和下載之前我們需要做一些準(zhǔn)備工作,在Apache官網(wǎng)去下載文件上傳下載的兩個(gè)組件,下載鏈接這里給出:common-fileupload組件下載:http://commons.apache.org/proper/commons-fileupload/

common-io組件下載:http://commons.apache.org/proper/commons-io/根據(jù)自己需求下載對(duì)應(yīng)版本

一、創(chuàng)建工程

將所需要的兩個(gè)開(kāi)發(fā)包導(dǎo)入到工程項(xiàng)目中如圖:

二、代碼編寫(xiě)

1.前端頁(yè)面代碼

1). 在WebRoot目錄下新建一個(gè)fileUpload.jsp頁(yè)面,用來(lái)上傳文件

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">

 <title>My JSP 'fileUpload.jsp' starting page</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>
 <!-- 文件上傳表單的提交方式必須是“post” 編碼類(lèi)型必須為:enctype="multipart/form-data" -->
 <form action="UploadServlet" method="post" enctype="multipart/form-data">

  username: <input type="text" name="username" /><br>
  file: <input type="file" name="file"><br>
  file2: <input type="file" name="file2"><br>
  <input type="submit" value="上傳文件">

 </form>

 </body>
</html>

2).新建一個(gè)fileUploadResult.jsp頁(yè)面用來(lái)顯示結(jié)果信息

<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">

 <title>My JSP 'fileUploadResult.jsp' starting page</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>
 <%--
 <%
  //獲取流對(duì)象
  InputStream inputStream = request.getInputStream();

  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

  String buffer = null;

  while((buffer = br.readLine()) != null){
   out.print(buffer + "<br>");
  }

  br.close();
  inputStream.close();

  %>
  --%>
  ${message}<br>

  EL-username : ${requestScope.username} <br>
  EL-file1 : ${requestScope.file }<br>
  EL-file2 : ${requestScope.file2}<br>

 </body>
</html>

2. 編寫(xiě)上傳文件處理的Servlet代碼

1) UploadServlet.java代碼如下:

package com.Servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {


  //得到上傳文件的保存目錄,將上傳的文件放在webRoot目錄下(但是一般為了安全放在WEB-INF目錄下,不允許外界直接訪(fǎng)問(wèn),保證上傳的安全)
  String path = this.getServletContext().getRealPath("/upload");

  File file = new File(path);

  //判斷上傳文件的保存目錄是否存在
  if(!file.exists() && !file.isDirectory()){
   System.out.println(path + "目錄不存在,需要?jiǎng)?chuàng)建!");
   //創(chuàng)建目錄
   file.mkdir();
  }
  //消息提示
  String message = "";
  try{
   //使用Apache文件上傳組件處理文件上傳步驟:
   //1.創(chuàng)建一個(gè)DiskFileItemFactory工廠
   DiskFileItemFactory factory = new DiskFileItemFactory();
   //2.創(chuàng)建一個(gè)文件上傳解析器
   ServletFileUpload upload = new ServletFileUpload(factory);
   //解決中文亂碼
   upload.setHeaderEncoding("UTF-8");
   //3.判斷提交的數(shù)據(jù)普通表單的數(shù)據(jù)還是帶文件上傳的表單
   if(!upload.isMultipartContent(request)){
    //如果是表單數(shù)據(jù)普通表單,則按照傳統(tǒng)方式獲取數(shù)據(jù)
    return ;
   }
   //4.使用ServletFileUpload解析器解析上傳數(shù)據(jù),解析結(jié)果返回的是一個(gè)List<FileItem>集合,每一個(gè)FileItem對(duì)應(yīng)一個(gè)Form表單的輸入項(xiàng)
   List<FileItem> list = upload.parseRequest(request);
   for(FileItem item : list){
    //如果fileItem中封裝的是普通輸入項(xiàng)的數(shù)據(jù)
    if(item.isFormField()){
     //獲取字段名字
     String name = item.getFieldName();
     //解決普通輸入項(xiàng)中中文亂碼問(wèn)題
     String value = item.getString("UTF-8");//value = new String(value.getBytes("iso8859-1"),"UTF-8");
     System.out.println(name + " = " + value);
    }else{ //如果表單中提交的是上傳文件
     //獲得上傳的文件名稱(chēng)
     String filename = item.getName();
     System.out.println(filename);
     if(filename == null || filename.trim().equals(" ")){
      continue;
     }
     //注意:不同的瀏覽器提交的文件名稱(chēng)是不一樣的,有些瀏覽器提交的文件會(huì)帶有路徑,如“D:\\project\WebRoot\hello.jsp”,有一些是單純的文件名:hello.jsp
     //去掉獲取到文件名中的路徑名,保留單純的文件名
     filename = filename.substring(filename.lastIndexOf("\\") + 1);
     //獲取item中的上傳文件的輸入流
     InputStream in = item.getInputStream();
     //創(chuàng)建一個(gè)文件輸入流
     FileOutputStream out = new FileOutputStream(path + "\\" + filename);
     //創(chuàng)建一個(gè)緩沖區(qū)
     byte buffer[] = new byte[1024];
     //判斷輸入流中的數(shù)據(jù)是否已經(jīng)讀取完畢的標(biāo)志位
     int len = 0;
     //循環(huán)將輸入流讀入到緩沖區(qū)當(dāng)中,(len = in.read(buffer)>0)就表示in里面還有數(shù)據(jù)存在
     while((len = in.read(buffer)) > 0){
      //使用FileOutputStream輸出流將緩沖區(qū)的數(shù)據(jù)寫(xiě)入到指定的目錄(path+"\\"+filename)當(dāng)中
      out.write(buffer, 0, len);
     }
     //關(guān)閉輸入流
     in.close();
     //關(guān)閉輸出流
     out.close();
     //刪除處理文件上傳生成的臨時(shí)文件
     item.delete();
     message = "文件上傳成功!";


    }
   }

  }catch(Exception e){
   message = "文件上傳失敗!";
   e.printStackTrace();
  }

  request.setAttribute(message, message);
  request.getRequestDispatcher("fileUploadResult.jsp").forward(request, response);

 }

}

2)web.xml文件中的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 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_3_0.xsd">
 <servlet>

 <servlet-name>UploadServlet</servlet-name>
 <servlet-class>com.Servlet.UploadServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>UploadServlet</servlet-name>
 <url-pattern>/UploadServlet</url-pattern>
 </servlet-mapping>

</web-app>

結(jié)果:

到此這篇關(guān)于eclipse java中文件的上傳和下載示例解析的文章就介紹到這了,更多相關(guān)eclipse java中文件的上傳和下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入理解JavaWeb中過(guò)濾器與監(jiān)聽(tīng)器的應(yīng)用

    深入理解JavaWeb中過(guò)濾器與監(jiān)聽(tīng)器的應(yīng)用

    這篇文章主要介紹了JavaWeb中過(guò)濾器與監(jiān)聽(tīng)器的應(yīng)用,過(guò)濾器能夠?qū)ζヅ涞恼?qǐng)求到達(dá)目標(biāo)之前或返回響應(yīng)之后增加一些處理代碼,監(jiān)聽(tīng)器是一個(gè)接口內(nèi)容由我們實(shí)現(xiàn),會(huì)在特定時(shí)間被調(diào)用,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)

    springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)

    本文會(huì)和SpringBoot做整合,實(shí)現(xiàn)RabbitMQ發(fā)送短信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存

    詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存

    本篇文章主要介紹了詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java遞歸算法簡(jiǎn)單示例兩則

    Java遞歸算法簡(jiǎn)單示例兩則

    這篇文章主要介紹了Java遞歸算法,通過(guò)兩則示例分析了Java遞歸算法實(shí)現(xiàn)階乘與求和的具體操作技巧,需要的朋友可以參考下
    2017-09-09
  • 微信小程序后端Java接口開(kāi)發(fā)的詳細(xì)步驟

    微信小程序后端Java接口開(kāi)發(fā)的詳細(xì)步驟

    現(xiàn)在微信小程序越來(lái)越火了,相信不少人都通過(guò)各種途徑學(xué)習(xí)過(guò)微信小程序或者嘗試開(kāi)發(fā),本文就介紹了微信小程序后端Java接口開(kāi)發(fā)的詳細(xì)步驟,感興趣的同學(xué)可以學(xué)習(xí)一下
    2021-11-11
  • java實(shí)現(xiàn)俄羅斯方塊

    java實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • JAVA注解代碼詳解一篇就夠了

    JAVA注解代碼詳解一篇就夠了

    這篇文章主要介紹了Java注解詳細(xì)介紹,本文講解了Java注解是什么、Java注解基礎(chǔ)知識(shí)、Java注解類(lèi)型、定義Java注解類(lèi)型的注意事項(xiàng)等內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 淺談StringBuilder類(lèi)的capacity()方法和length()方法的一些小坑

    淺談StringBuilder類(lèi)的capacity()方法和length()方法的一些小坑

    這篇文章主要介紹了StringBuilder類(lèi)的capacity()方法和length()方法的一些小坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Springmvc ModelAndView原理及用法詳解

    Springmvc ModelAndView原理及用法詳解

    這篇文章主要介紹了Springmvc ModelAndView原理及用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟

    JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟

    這篇文章主要介紹了JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟,通過(guò)將數(shù)據(jù)庫(kù)的連接放在一個(gè)工具類(lèi)里面,達(dá)到重用的效果,需要的朋友可以參考下
    2015-07-07

最新評(píng)論

龙海市| 蓝田县| 柞水县| 长沙县| 顺平县| 阆中市| 徐州市| 嘉定区| 电白县| 宜兰县| 安义县| 浦江县| 屯昌县| 临安市| 佛坪县| 绩溪县| 广宁县| 东光县| 高碑店市| 北海市| 宜兰市| 灵宝市| 五华县| 昌黎县| 龙海市| 民县| 义马市| 玉环县| 孝感市| 太湖县| 博罗县| 罗甸县| 建瓯市| 河津市| 沁源县| 博白县| 兴宁市| 黄骅市| 赤水市| 黄石市| 大石桥市|