Servlet3.0上傳檔案

來源:互聯網
上載者:User

標籤:print   大小   isp   position   nbsp   throw   web   duti   檔案大小   

 1 package com.itheima.upload; 2  3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8  9 import javax.servlet.ServletException;10 import javax.servlet.annotation.MultipartConfig;11 import javax.servlet.annotation.WebServlet;12 import javax.servlet.http.HttpServlet;13 import javax.servlet.http.HttpServletRequest;14 import javax.servlet.http.HttpServletResponse;15 import javax.servlet.http.Part;16 17 import com.itheima.utils.UUIDUtils;18 import com.itheima.utils.UploadUtils;19 /**20  * 檔案上傳的Servlet21  * 表單項:分為兩類 一類是檔案表單項  一類是非檔案表單項(普通表單項,不必設定enctype="mutipart/form-data")22  */23 @MultipartConfig24 @WebServlet("/UploadServlet")25 public class UploadServlet extends HttpServlet {26     private static final long serialVersionUID = 1L;27     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {28         //接受普通資料 非檔案表單項 即普通表單項 不必設定enctype="multipart/form-data"29         request.setCharacterEncoding("UTF-8");30         String filedesc = request.getParameter("filedesc");31         System.out.println("檔案描述:"+filedesc);32         //接受檔案 檔案表單項 必須滿足三要素 post form enctype="multipart/form-data"33         //通過表單file控制項(<input type="file" name="upload">)的名字屬性直接擷取Part對象34         Part part = request.getPart("upload");35         //獲得檔案大小36         long size = part.getSize();37         System.out.println("檔案的大小為:"+size);38         String name = part.getName();39         System.out.println("檔案上傳表單的name屬性的值為:"+name);40         //擷取頭資訊41         String header = part.getHeader("Content-Disposition");42         System.out.println(header);43         //擷取檔案名稱字 \"為轉義 44         int lastIndexOf = header.lastIndexOf("filename=\"");45         String filename = header.substring(lastIndexOf+10, header.length()-1);46         System.out.println("檔案名稱:"+filename);47         //獲得檔案內容48         InputStream ips = part.getInputStream();49         //擷取要上傳到的檔案夾路徑50         String realPath = this.getServletContext().getRealPath("/upload");51         System.out.println(realPath);52         //獲得唯一檔案名53         String uniname = UUIDUtils.getUUIDFileName(filename);54         //獲得新的上傳路徑55         String fenglipath = realPath + UploadUtils.getPath(uniname);56         System.out.println(fenglipath);57         //建立上傳路徑的file對象58         File file = new File(fenglipath);59         //如果上傳路徑不存在,就建立60         if(!file.exists()){61             file.mkdirs();62         }63         //建立位元組輸出資料流,參數為"路徑+檔案名稱"64         OutputStream ops = new FileOutputStream(fenglipath+"/"+uniname);65         //io流讀寫66         byte [] bytes = new byte [1024];67         int len;68         while((len=ips.read(bytes))!=-1){69             ops.write(bytes, 0, len);70         }71         //關流72         ips.close();73         ops.close();74         75         76     }77     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {78         doGet(request, response);79     }80 81 }

 

Servlet3.0上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.