成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java基于servlet實(shí)現(xiàn)文件上傳功能

本文實(shí)例為大家分享了java基于servlet實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括歷下網(wǎng)站建設(shè)、歷下網(wǎng)站制作、歷下網(wǎng)頁制作以及歷下網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,歷下網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到歷下省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

研究了一天終于將java上傳文件看懂了,當(dāng)然懂的僅僅是皮毛,不妨記下來防止以后忘了。

我們在網(wǎng)上看關(guān)于文件的上傳有很多的介紹,當(dāng)然有的可以使用有的則不合適:我們首先來看前臺(tái)的界面。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>上傳文件</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
 <div class="container kv-main">
 <div class="page-header">
 <h2>上傳文件</h2>
 </div> 
 <form enctype="multipart/form-data" action="../upload?method=uploadPic" method="post"> 
  <input name="file-1" type="file"><br> 
  <button type="submit" class="btn btn-primary">Submit</button>
  <button type="reset" class="btn btn-default">Reset</button>
 </form> 
 <hr> 
 <br> 
 </div> 
</body>
</html>

這個(gè)地方是為了好看使用了Bootstrap進(jìn)行布局,如下:

java基于servlet實(shí)現(xiàn)文件上傳功能

在做好前臺(tái)的頁面之后,我們開看后臺(tái)的代碼,在看servlet之前我們首先來看看web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>myShop</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 
 <servlet>
 <servlet-name>upload</servlet-name>
 <servlet-class>com.epoint.shop.fileupload.UpLoadServlet</servlet-class>
 <init-param>
 <param-name>filePath</param-name>
 <param-value>E://workspace1/myShop/WebContent/upload</param-value>
 </init-param>
 <init-param>
 <param-name>tempFilePath</param-name>
 <param-value>temp</param-value>
 </init-param>
 </servlet>
 
 <servlet-mapping>
 <servlet-name>upload</servlet-name>
 <url-pattern>/upload</url-pattern>
 </servlet-mapping>
</web-app>

為什么要配置web.xml,是因?yàn)槲覀冊趕ervlet有一個(gè)要獲取文件存放的路徑,我們不妨將這個(gè)路徑存放到web.xml這個(gè)我們當(dāng)我們的項(xiàng)目進(jìn)行平臺(tái)的遷移的時(shí)候,我們需要改動(dòng)的只是web.xml不需要給java內(nèi)部的核心的代碼。

然后我們來看servlet的代碼:

package com.epoint.shop.fileupload;
 
import java.io.File;
import java.io.IOException;
 
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/UpLoad")
public class UpLoadServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private static String TEMP_FOLDER="/upload";
 // 上傳配置
 private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
 private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
 private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
 private String filePath; //存放上傳文件的目錄
 private String tempFilePath; //存放臨時(shí)文件的目錄
 
 public UpLoadServlet() {
 super();
 
 }
 public void init(ServletConfig config)throws ServletException {
 super.init(config);
 filePath=config.getInitParameter("filePath");
 System.out.println(filePath);
 }
 
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doPost(request, response);
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("utf-8"); 
 response.setCharacterEncoding("utf-8");
 String methodName=request.getParameter("method");
 if(methodName!=null){
 try {
  Method method=this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
 method.invoke(this, request,response);
 } catch (Exception e) {
 e.printStackTrace();
 }
  
 
 }
 }
 public void uploadPic(HttpServletRequest request, HttpServletResponse response) throws Exception{
 
 //檢測是否為多媒體上傳
 
 if(!ServletFileUpload.isMultipartContent(request)){
 //如果不是就停止
 PrintWriter writer=response.getWriter();
 writer.println("表單中必須包含enctype=multipart/form-data");
 writer.flush();
 return;
 }
 DiskFileItemFactory factory = new DiskFileItemFactory(); // 創(chuàng)建文件項(xiàng)目工廠對象
 factory.setRepository(new File(savePath)); //設(shè)置臨時(shí)文件夾為TEMP_FOLDER
 factory.setSizeThreshold(1024 * 1024);  // 設(shè)置緩沖區(qū)大小為 1M
 // 構(gòu)造臨時(shí)路徑來存儲(chǔ)上傳的文件
 ServletFileUpload upload = new ServletFileUpload(factory);//用工廠實(shí)例化上傳組件,ServletFileUpload用來解析文件上傳請求 
 // 設(shè)置最大文件上傳值
 upload.setFileSizeMax(MAX_FILE_SIZE);
 // 設(shè)置最大請求值 (包含文件和表單數(shù)據(jù))
 upload.setSizeMax(MAX_REQUEST_SIZE);
 upload.setHeaderEncoding("UTF-8"); 
 @SuppressWarnings("unchecked")
 List<FileItem> list = upload.parseRequest(request);
 if (list != null && list.size() > 0) {
  for (FileItem item : list) {
  if (!item.isFormField()) {
   String fileName = new File(item.getName()).getName();
   File storeFile = new File(filePath,fileName);
   // 保存文件到硬盤
   item.write(storeFile);
   request.setAttribute("message","文件上傳成功!");
  }
  }
 }
 } 
}

在今天上午我在編寫的過程中遇到一個(gè)問題,就是有一個(gè)

List<FileItem> list = upload.parseRequest(request);

就這個(gè)代碼獲取到的list一直為空,如果你也遇到這樣的問題,可以配置一下web.xml,因?yàn)槲覀僼omcat可能會(huì)對文件進(jìn)行攔截,但是我也不清楚為什么在配置了web.xml之后,就可以上傳文件了。你如果遇到這樣的問題不妨是試一試,請求通過web.xml的mapping進(jìn)行轉(zhuǎn)發(fā)。

還有文件的上傳需要兩個(gè)包,別忘了

java基于servlet實(shí)現(xiàn)文件上傳功能

上面畫圓圈的就是了,希望對你有所幫助。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享題目:java基于servlet實(shí)現(xiàn)文件上傳功能
當(dāng)前網(wǎng)址:http://jinyejixie.com/article36/poccsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、ChatGPT、建站公司、品牌網(wǎng)站建設(shè)App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
白朗县| 凤凰县| 应城市| 上思县| 德令哈市| 石家庄市| 福鼎市| 射阳县| 右玉县| 固阳县| 蕲春县| 东乌珠穆沁旗| 仙居县| 右玉县| 陆良县| 永春县| 元江| 连城县| 兴和县| 金川县| 鄂托克前旗| 前郭尔| 抚顺县| 南宁市| 雷波县| 海城市| 清苑县| 喀喇沁旗| 泗阳县| 大石桥市| 大丰市| 镶黄旗| 阿克陶县| 温泉县| 麟游县| 阿拉尔市| 平罗县| 尼玛县| 黄冈市| 尚义县| 阳春市|