日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 文件上传 demo_java文件上传Demo

發布時間:2025/3/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 文件上传 demo_java文件上传Demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說到文件上傳我們要做到:

1.引入兩個包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar

2.將form改為上傳文件模式:enctype="multipart/form-data"

3.開始編寫相關代碼

這里會用到幾個關鍵的類:磁盤文件工廠DiskFileItemFactory ;?創建servlet文件上傳類:ServletFileUpload

還有幾個重要的方法:DiskFileItemFactory類用于將以臨時文件形式保存在磁盤上的存放目錄的方法setRepository;

ServletFileUpload類得到表單中所有的數據,得到form表單中所有的元素方法:parseRequest

下面看具體代碼:

說明以這種方式上傳文件是保存在服務器端的!

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.UUID;

import javax.servlet.ServletException;

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.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.io.FileUtils;

public class UploadServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public UploadServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

DiskFileItemFactory sf= new DiskFileItemFactory();//實例化磁盤被文件列表工廠

String path = request.getRealPath("/upload");//得到上傳文件的存放目錄

sf.setRepository(new File(path));//設置文件存放目錄

sf.setSizeThreshold(1024*1024);//設置文件上傳小于1M放在內存中

String rename = "";//文件新生成的文件名

String fileName = "";//文件原名稱

String name = "";//普通field字段

//從工廠得到servletupload文件上傳類

ServletFileUpload sfu = new ServletFileUpload(sf);

try {

List lst = sfu.parseRequest(request);//得到request中所有的元素

for (FileItem fileItem : lst) {

if(fileItem.isFormField()){

if("name".equals(fileItem.getFieldName())){

name = fileItem.getString("UTF-8");

}

}else{

//獲得文件名稱

fileName = fileItem.getName();

fileName = fileName.substring(fileName.lastIndexOf("\\")+1);

String houzhui = fileName.substring(fileName.lastIndexOf("."));

rename = UUID.randomUUID()+houzhui;

fileItem.write(new File(path, rename));

}

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("普通字段"+name);

System.out.println("文件名稱"+fileName);

System.out.println("修改后生成的文件名稱"+rename);

response.sendRedirect("ok.jsp");

out.flush();

out.close();

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

index.jsp頁面:

文件測試界面

名稱:

圖片:

ok.jsp頁面:

上傳文件成功!

實現效果就不截圖了,有需要自己那過去用!

總結

以上是生活随笔為你收集整理的java 文件上传 demo_java文件上传Demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。