欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javaweb實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2022年06月22日 11:05:22   作者:深藍(lán)夢(mèng)夕陽  
這篇文章主要為大家詳細(xì)介紹了javaweb實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

1、創(chuàng)建一個(gè)空項(xiàng)目

2、新建一個(gè)web application 的Module

3、創(chuàng)建一個(gè)lib目錄導(dǎo)入需要用的jar包

  • commons-io
  • commons-fileupload

4、將lib包添加到項(xiàng)目依賴(右鍵 Add as Library)

5、編寫文件上傳表單

<%--通過表單上傳文件
? ? get : 上傳文件大小有限制
? ? post : 上傳文件大小沒有限制
? ? 上傳文件必須要enctype="multipart/form-data"
--%>

? <form action="${pageContext.request.contextPath}/upload.do" method="post" enctype="multipart/form-data">
? ? <p>上傳用戶:<input type="text" name="username"></p>
? ? <p><input type="file" name="file1"></p>
? ? <p><input type="submit"> | <input type="reset"></p>
? </form>

6.編寫Servlet

public class FileServlet extends HttpServlet {
? ? protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

? ? ? ? //判斷上傳的表單是普通表單還是帶文件表單
? ? ? ? if (!ServletFileUpload.isMultipartContent(request)){//如果不是帶文件表單
? ? ? ? ? ? return;//終止方法運(yùn)行,直接返回
? ? ? ? }

? ? ? ? try {
? ? ? ? //創(chuàng)建上傳文件的保存路徑,建議在WEB-INF路徑下,安全,用戶無法直接訪問上傳的文件.
? ? ? ? String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
? ? ? ? File uploadFile = new File(uploadPath);
? ? ? ? if (!uploadFile.exists()){
? ? ? ? ? ? uploadFile.mkdirs();//如果不存在則創(chuàng)建目錄
? ? ? ? }

? ? ? ? //緩存,臨時(shí)文件
? ? ? ? //臨時(shí)文件,假如文件超出預(yù)期大小,就把它放到臨時(shí)文件夾中,過幾天自動(dòng)刪除,或者提醒用戶轉(zhuǎn)存為永久文件
? ? ? ? String tmpPath = this.getServletContext().getRealPath("/WEB-INF/tmp");
? ? ? ? File tmpFile = new File(tmpPath);
? ? ? ? if (!tmpFile.exists()){
? ? ? ? ? ? tmpFile.mkdirs();//如果不存在則創(chuàng)建臨時(shí)目錄
? ? ? ? }

? ? ? ? //1.創(chuàng)建DiskFileItemFactory對(duì)象,處理文件上傳路徑或者大小限制
? ? ? ? DiskFileItemFactory factory = new DiskFileItemFactory();
? ? ? ? //通過這個(gè)工廠設(shè)置一個(gè)緩沖區(qū),當(dāng)上傳的文件大于這個(gè)緩沖區(qū)的時(shí)候,將它放在臨時(shí)文件中
? ? ? ? factory.setSizeThreshold(1024*1024);//緩沖區(qū)大小為1M
? ? ? ? factory.setRepository(tmpFile);//臨時(shí)文件的保存目錄

? ? ? ? //2.獲取ServletFileUpload對(duì)象
? ? ? ? ServletFileUpload upload = new ServletFileUpload(factory);

? ? ? ? //監(jiān)聽文件上傳進(jìn)度
? ? ? ? upload.setProgressListener(new ProgressListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void update(long l, long l1, int i) {
? ? ? ? ? ? ? ? System.out.println("總大小: "+l1+" 已上傳: "+l);
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? //處理亂碼問題
? ? ? ? upload.setHeaderEncoding("utf-8");
? ? ? ? //設(shè)置單個(gè)文件的最大值
? ? ? ? upload.setFileSizeMax(1024*1024*10);//10M
? ? ? ? //設(shè)置總共能夠上傳文件的大小
? ? ? ? upload.setSizeMax(1024*1024*10);//10M

? ? ? ? //3.處理上傳文件
? ? ? ? ? ? //把前端請(qǐng)求解析,封裝成一個(gè)FileItem對(duì)象
? ? ? ? ? ? List<FileItem> fileItems = upload.parseRequest(request);

? ? ? ? ? ? for (FileItem fileItem : fileItems) {
? ? ? ? ? ? ? ? //判斷上傳的表單是普通表單還是帶文件表單
? ? ? ? ? ? ? ? if (fileItem.isFormField()){
? ? ? ? ? ? ? ? ? ? String name=fileItem.getFieldName();//獲取表單控件的名字
? ? ? ? ? ? ? ? ? ? String value=fileItem.getString("UTF-8");//獲取值,處理亂碼
? ? ? ? ? ? ? ? ? ? System.out.println(name+": "+value);
? ? ? ? ? ? ? ? }else {//文件
? ? ? ? ? ? ? ? ? ? String uploadFileName = fileItem.getName();//獲取上傳文件名字(帶路徑)
? ? ? ? ? ? ? ? ? ? //可能存在文件名不合法的情況
? ? ? ? ? ? ? ? ? ? if (uploadFileName==null||uploadFileName.trim().equals("")){
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //截取上傳的文件名
? ? ? ? ? ? ? ? ? ? String FileName=uploadFileName.substring(uploadFileName.lastIndexOf("/")+1);//從最后一個(gè)/后開始截取
? ? ? ? ? ? ? ? ? ? //截取后綴名
? ? ? ? ? ? ? ? ? ? String fileExtName=uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);//從最后一個(gè).后開始截取

? ? ? ? ? ? ? ? ? ? //網(wǎng)絡(luò)傳輸中的東西,都需要序列化
? ? ? ? ? ? ? ? ? ? //POJO , 實(shí)體類, 如果想要在多個(gè)電腦運(yùn)行, 傳輸-->需要把對(duì)象序列化
? ? ? ? ? ? ? ? ? ? //JNI= java native Interface
? ? ? ? ? ? ? ? ? ? //implements Serializable : 標(biāo)記接口 , JVM-->java棧 本地方法棧 native -->C++

? ? ? ? ? ? ? ? ? ? //可以使用UUID(唯一標(biāo)識(shí)的通用碼),保證文件名唯一
? ? ? ? ? ? ? ? ? ? String uuidPath = UUID.randomUUID().toString();//生成一共隨機(jī)的uuid

? ? ? ? ? ? ? ? ? ? //==========================創(chuàng)建存放目錄========================//
? ? ? ? ? ? ? ? ? ? String realPath= uploadPath+"/"+uuidPath;
? ? ? ? ? ? ? ? ? ? //給每個(gè)文件創(chuàng)建一個(gè)對(duì)應(yīng)的文件夾
? ? ? ? ? ? ? ? ? ? File realPathFile = new File(realPath);
? ? ? ? ? ? ? ? ? ? if (!realPathFile.exists()){
? ? ? ? ? ? ? ? ? ? ? ? realPathFile.mkdirs();
? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? //==========================文件傳輸====================================//
? ? ? ? ? ? ? ? ? ? //獲取文件上傳的流
? ? ? ? ? ? ? ? ? ? InputStream inputStream = fileItem.getInputStream();
? ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)輸出文件的流
? ? ? ? ? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(realPath + "/" + FileName);
? ? ? ? ? ? ? ? ? ? //創(chuàng)建緩沖區(qū)
? ? ? ? ? ? ? ? ? ? byte[] buffer=new byte[1024];
? ? ? ? ? ? ? ? ? ? //判斷是否讀取完畢
? ? ? ? ? ? ? ? ? ? int len=0;
? ? ? ? ? ? ? ? ? ? while ((len=inputStream.read(buffer))>0){
? ? ? ? ? ? ? ? ? ? ? ? fos.write(buffer,0,len);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //關(guān)閉流
? ? ? ? ? ? ? ? ? ? fos.close();
? ? ? ? ? ? ? ? ? ? inputStream.close();

? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (FileUploadException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

7.注冊(cè)Servlet

<servlet>
? ? <servlet-name>FileServlet</servlet-name>
? ? <servlet-class>com.kuang.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
? ? <servlet-name>FileServlet</servlet-name>
? ? <url-pattern>/upload.do</url-pattern>
</servlet-mapping>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論