JavaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
本文實(shí)例為大家分享了JavaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
1.概述
通常瀏覽器上傳的所有參數(shù),我們可以通過request對(duì)象的getParameter , getParameterMap , getParameterValue 這三個(gè)方法拿到所有的請(qǐng)求參數(shù),
但有一種情況,當(dāng)強(qiáng)求包含參數(shù)包含文件上傳時(shí), 這三個(gè)方法都失效,無法拿到參數(shù),
我們就需要request對(duì)象的getInputStream方法獲取這些參數(shù), 如何解析這個(gè)字節(jié)輸入流呢?
apache 軟件基金會(huì): 開發(fā)了工具fileupload工具, 專門解析字節(jié)輸入流,實(shí)現(xiàn)文件上傳功能.
2. 先導(dǎo)入jar包
2.1打開pom文件, 加入fileupload的jar包依賴.

2.2 三要素
1.必須post請(qǐng)求
2.form表單屬性必須包含 enctype=“multipart/form-data”
3.上傳文本框input type=“file” , 必須有name屬性

2.3 代碼邏輯
自定義一個(gè)parseRequest(request)方法, 返回map集合,map集合中封裝了商品添加功能中提交所有的數(shù)據(jù);
使用BeanUtils.populate(product,parameterMap)方法將map集合封裝到product對(duì)象中; 調(diào)用業(yè)務(wù)層addProduct方法傳遞product對(duì)象參數(shù),在dao層將新添加的商品寫入數(shù)據(jù)庫;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ? Map<String, String[]> parameterMap = parseRequest(request); ?//request.getParameterMap();
? ? ? ? Product product ?= new Product();
? ? ? ? try {
? ? ? ? ? ? BeanUtils.populate(product,parameterMap);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? productService.addProduct(product);
? ? ? ? ResultVO resultVO = new ResultVO(ResultVO.SUCCESS,null,"商品添加成功");
? ? ? ? response.getWriter().print(objectMapper.writeValueAsString(resultVO));
? ? }實(shí)現(xiàn)parseRequest(request)方法, 實(shí)現(xiàn)文件上傳功能:
private Map<String,String[]> parseRequest(HttpServletRequest request) {
? ? ? ? Map<String,String[]> map = new HashMap<String, String[]>();
? ? ? ? //創(chuàng)建對(duì)象,磁盤工廠對(duì)象
? ? ? ? DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
? ? ? ? //創(chuàng)建負(fù)責(zé)解析Request流的對(duì)象,構(gòu)造方法,傳遞磁盤工廠對(duì)象
? ? ? ? ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
? ? ? ? //獲取當(dāng)前日期,文件夾名字使用
? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
? ? ? ? String stringDate = sdf.format(new Date());
? ? ? ? try {
? ? ? ? ? ? //解析對(duì)象的方法,parseRequest,解析reqeust對(duì)象
? ? ? ? ? ? //返回集合 List, 集合的泛型,是另一個(gè)對(duì)象: 文件項(xiàng)對(duì)象
? ? ? ? ? ? //文件項(xiàng)對(duì)象: 客戶端提交的任何數(shù)據(jù),都認(rèn)為是一個(gè)文件項(xiàng) (普通文本框,附件上傳)
? ? ? ? ? ? //FileItem 客戶端的每一個(gè)上傳數(shù)據(jù) (當(dāng)前案例是7 個(gè) 文件項(xiàng))
? ? ? ? ? ? List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
? ? ? ? ? ? //遍歷集合,獲取每個(gè)文件項(xiàng)對(duì)象 ?fileItem
? ? ? ? ? ? for(FileItem fileItem : fileItemList){
? ? ? ? ? ? ? ? //判斷 ?fileItem文件項(xiàng)對(duì)象方法,判斷出當(dāng)前的文件項(xiàng)是普通文本,還是附件
? ? ? ? ? ? ? ? if (fileItem.isFormField()){ //isFormField()返回true,普通項(xiàng) , 返回false 是附件
? ? ? ? ? ? ? ? ? ? //普通文本,取出用戶在文本框輸入的數(shù)據(jù),帶編碼表名
? ? ? ? ? ? ? ? ? ? String s = fileItem.getString("utf-8");
? ? ? ? ? ? ? ? ? ? //方法: 獲取表單input的name的屬性值
? ? ? ? ? ? ? ? ? ? String name = fileItem.getFieldName();
? ? ? ? ? ? ? ? ? ?//System.out.println(name+"==="+s);
? ? ? ? ? ? ? ? ? ? //數(shù)據(jù),封裝到Map集合
? ? ? ? ? ? ? ? ? ? map.put(name,new String[]{s});
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? //方法isFormField()返回false,是附件項(xiàng)
? ? ? ? ? ? ? ? ? ? //FileItem對(duì)象方法 getName()獲取到上傳的文件名
? ? ? ? ? ? ? ? ? ? String fileName = fileItem.getName();
? ? ? ? ? ? ? ? ? ? //文件上傳,是需要修改文件名
? ? ? ? ? ? ? ? ? ? //System.out.println(fileName); // Jellyfish.jpg
? ? ? ? ? ? ? ? ? ? //獲取文件的后綴名,獲取文件名 . 出現(xiàn)的索引
? ? ? ? ? ? ? ? ? ? int index = ?fileName.lastIndexOf(".");
? ? ? ? ? ? ? ? ? ? //截取字符串
? ? ? ? ? ? ? ? ? ? fileName = fileName.substring(index);
? ? ? ? ? ? ? ? ? ? //System.out.println(fileName);
? ? ? ? ? ? ? ? ? ? //自定義新的文件名
? ? ? ? ? ? ? ? ? ? fileName = "itheima"+System.currentTimeMillis()+ UUIDUtil.getId()+fileName;
? ? ? ? ? ? ? ? ? ? //System.out.println(fileName);
? ? ? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ? ? ?* 處理的上傳路徑,項(xiàng)目的目錄 E:\heima364\store\src\main\webapp\web\resources\products
? ? ? ? ? ? ? ? ? ? ?* 開發(fā)的項(xiàng)目: 跨平臺(tái)運(yùn)行
? ? ? ? ? ? ? ? ? ? ?* Windows 系統(tǒng)路徑 ?E:\heima364\store\src\main\webapp\web\resources\products
? ? ? ? ? ? ? ? ? ? ?* Linux 操作系統(tǒng) /ss/ss/ss/ss
? ? ? ? ? ? ? ? ? ? ?* 路徑定義在配置文件中,讀取
? ? ? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? ? ? ResourceBundle bundle = ResourceBundle.getBundle("uploadPath");
? ? ? ? ? ? ? ? ? ? //上傳路徑,讀取配置文件
? ? ? ? ? ? ? ? ? ? String path = bundle.getString("path");
? ? ? ? ? ? ? ? ? ? //E:/heima364/store/src/main/webapp/web/resources/products /stringDate
? ? ? ? ? ? ? ? ? ? //File對(duì)象實(shí)現(xiàn),路徑的合并 (上傳路徑path,和日期字符串合并為一個(gè)路徑)
? ? ? ? ? ? ? ? ? ? File uploadDir = new File(path,stringDate); //E:\heima364\store\src\main\webapp\web\resources\products\2020-02-27
? ? ? ? ? ? ? ? ? ? //判斷該路徑是否存在
? ? ? ? ? ? ? ? ? ? if (!uploadDir.exists()){
? ? ? ? ? ? ? ? ? ? ? ? //不存在,就創(chuàng)建
? ? ? ? ? ? ? ? ? ? ? ? uploadDir.mkdirs();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //上傳的路徑 uploadDir 和文件名 fileName,組成一個(gè)新的File對(duì)象
? ? ? ? ? ? ? ? ? ? //E:\heima364\store\src\main\webapp\web\resources\products\2020-02-27\itheima158279119698617ad226bf52d4eb4bc9cd97dbbd1fd5a.jpg
? ? ? ? ? ? ? ? ? ? File uploadDirFile = new File(uploadDir,fileName);
? ? ? ? ? ? ? ? ? ? //文件賦值,字節(jié)流讀取文件
? ? ? ? ? ? ? ? ? ? //文件項(xiàng)對(duì)象的方法, getInputStream獲取輸入流,讀取的是上傳的文件
? ? ? ? ? ? ? ? ? ? InputStream inputStream = fileItem.getInputStream();
? ? ? ? ? ? ? ? ? ? //字節(jié)的輸出流
? ? ? ? ? ? ? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(uploadDirFile);
? ? ? ? ? ? ? ? ? ? //commonsIO工具的方法來實(shí)現(xiàn)
? ? ? ? ? ? ? ? ? ? IOUtils.copy(inputStream,fileOutputStream);
? ? ? ? ? ? ? ? ? ? fileOutputStream.close();
? ? ? ? ? ? ? ? ? ? //從上傳的路徑中uploadDirFile,獲取出一部分路徑,寫入到數(shù)據(jù)庫
? ? ? ? ? ? ? ? ? ? //File對(duì)象的方法toString(),路徑轉(zhuǎn)成字符串
? ? ? ? ? ? ? ? ? ? //獲取resources出現(xiàn)的索引
? ? ? ? ? ? ? ? ? ? index = uploadDirFile.toString().indexOf("resources");
? ? ? ? ? ? ? ? ? ? String pimage = uploadDirFile.toString().substring(index);
? ? ? ? ? ? ? ? ? ? //替換路徑中的 /
? ? ? ? ? ? ? ? ? ? pimage =pimage.replace("\\","/");
? ? ? ? ? ? ? ? ? ? //路徑,封裝到Map集合中/
? ? ? ? ? ? ? ? ? ? map.put("pimage",new String[]{pimage});
? ? ? ? ? ? ? ? ? ? fileItem.delete();//刪除上傳的臨時(shí)文件
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch (Exception ex){
? ? ? ? ? ? ex.printStackTrace();
? ? ? ? }
? ? ? ? //手動(dòng)封裝Map中缺少的數(shù)據(jù)
? ? ? ? //商品主鍵
? ? ? ? map.put("pid",new String[]{UUIDUtil.getId()});
? ? ? ? //上架,固定為0
? ? ? ? map.put("pflag",new String[]{"0"});
? ? ? ? //商品的發(fā)布日期
? ? ? ? map.put("pdate",new String[]{stringDate});
? ? ? ? return map;
? ? }以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?SPI?機(jī)制和實(shí)現(xiàn)自定義?starter
這篇文章主要介紹了SpringBoot?SPI機(jī)制和實(shí)現(xiàn)自定義?starter,全稱是Service?Provider?Interface。簡(jiǎn)單翻譯的話,就是服務(wù)提供者接口,是一種尋找服務(wù)實(shí)現(xiàn)的機(jī)制2022-08-08
SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
springboot自定義日志注解的實(shí)現(xiàn)
本文主要介紹了springboot自定義日志注解的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明
這篇文章主要介紹了關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Springboot JPA 枚舉Enum類型存入到數(shù)據(jù)庫的操作
這篇文章主要介紹了Springboot JPA 枚舉Enum類型存入到數(shù)據(jù)庫的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)操作
這篇文章主要介紹了spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
pdf2swf+flexpapers實(shí)現(xiàn)類似百度文庫pdf在線閱讀
這篇文章主要介紹了pdf2swf+flexpapers實(shí)現(xiàn)類似百度文庫pdf在線閱讀的相關(guān)資料,需要的朋友可以參考下2014-10-10
Springboot視頻接口報(bào)大量的ClientAbortException找不到原因的解決
本文主要介紹了Springboot視頻接口報(bào)大量的ClientAbortException找不到原因的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08

