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

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

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

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

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

在做好前臺的頁面之后,我們開看后臺的代碼,在看servlet之前我們首先來看看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,是因為我們在servlet有一個要獲取文件存放的路徑,我們不妨將這個路徑存放到web.xml這個我們當(dāng)我們的項目進(jìn)行平臺的遷移的時候,我們需要改動的只是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; //存放臨時文件的目錄    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)建文件項目工廠對象   factory.setRepository(new File(savePath));    //設(shè)置臨時文件夾為TEMP_FOLDER   factory.setSizeThreshold(1024 * 1024);     // 設(shè)置緩沖區(qū)大小為 1M    // 構(gòu)造臨時路徑來存儲上傳的文件   ServletFileUpload upload = new ServletFileUpload(factory);//用工廠實例化上傳組件,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","文件上傳成功!");        }      }    }  } }

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

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

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

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

分享文章:java中怎么利用servlet實現(xiàn)文件上傳功能-創(chuàng)新互聯(lián)
URL標(biāo)題:http://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)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

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