欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java中怎么利用servlet實(shí)現(xiàn)文件上傳功能-創(chuàng)新互聯(lián)

本篇文章為大家展示了java中怎么利用servlet實(shí)現(xiàn)文件上傳功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

龍游網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><title>上傳文件</title><link href="../css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"><script src="../js/jquery.min.js"></script><script src="../js/bootstrap.min.js" type="text/javascript"></script></head><body> <p class="container kv-main"> <p class="page-header">  <h2>上傳文件</h2> </p>     <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>   </p> </body></html>

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

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

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/tupian/20230522/ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee /tupian/20230522/ 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)槲覀冊(cè)趕ervlet有一個(gè)要獲取文件存放的路徑,我們不妨將這個(gè)路徑存放到web.xml這個(gè)我們當(dāng)我們的項(xiàng)目進(jìn)行平臺(tái)的遷移的時(shí)候,我們需要改動(dòng)的只是web.xml不需要給java內(nèi)部的核心的代碼。

然后我們來(lá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{  //檢測(cè)是否為多媒體上傳  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)目工廠(chǎng)對(duì)象   factory.setRepository(new File(savePath));    //設(shè)置臨時(shí)文件夾為T(mén)EMP_FOLDER   factory.setSizeThreshold(1024 * 1024);     // 設(shè)置緩沖區(qū)大小為 1M    // 構(gòu)造臨時(shí)路徑來(lái)存儲(chǔ)上傳的文件   ServletFileUpload upload = new ServletFileUpload(factory);//用工廠(chǎng)實(shí)例化上傳組件,ServletFileUpload用來(lái)解析文件上傳請(qǐng)求    // 設(shè)置較大文件上傳值    upload.setFileSizeMax(MAX_FILE_SIZE);    // 設(shè)置較大請(qǐng)求值 (包含文件和表單數(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);          // 保存文件到硬盤(pán)          item.write(storeFile);          request.setAttribute("message","文件上傳成功!");        }      }    }  } }

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

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

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

上述內(nèi)容就是java中怎么利用servlet實(shí)現(xiàn)文件上傳功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:java中怎么利用servlet實(shí)現(xiàn)文件上傳功能-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article38/desesp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)自適應(yīng)網(wǎng)站企業(yè)網(wǎng)站制作網(wǎng)站導(dǎo)航標(biāo)簽優(yōu)化云服務(wù)器

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司