JavaWeb實現簡單文件上傳功能
本文實例為大家分享了JavaWeb實現簡單文件上傳的具體代碼,供大家參考,具體內容如下
1.概述
通常瀏覽器上傳的所有參數,我們可以通過request對象的getParameter , getParameterMap , getParameterValue 這三個方法拿到所有的請求參數,
但有一種情況,當強求包含參數包含文件上傳時, 這三個方法都失效,無法拿到參數,
我們就需要request對象的getInputStream方法獲取這些參數, 如何解析這個字節(jié)輸入流呢?
apache 軟件基金會: 開發(fā)了工具fileupload工具, 專門解析字節(jié)輸入流,實現文件上傳功能.
2. 先導入jar包
2.1打開pom文件, 加入fileupload的jar包依賴.
2.2 三要素
1.必須post請求
2.form表單屬性必須包含 enctype=“multipart/form-data”
3.上傳文本框input type=“file” , 必須有name屬性
2.3 代碼邏輯
自定義一個parseRequest(request)方法, 返回map集合,map集合中封裝了商品添加功能中提交所有的數據;
使用BeanUtils.populate(product,parameterMap)方法將map集合封裝到product對象中; 調用業(yè)務層addProduct方法傳遞product對象參數,在dao層將新添加的商品寫入數據庫;
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)); ? ? }
實現parseRequest(request)方法, 實現文件上傳功能:
private Map<String,String[]> parseRequest(HttpServletRequest request) { ? ? ? ? Map<String,String[]> map = new HashMap<String, String[]>(); ? ? ? ? //創(chuàng)建對象,磁盤工廠對象 ? ? ? ? DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ? ? ? ? //創(chuàng)建負責解析Request流的對象,構造方法,傳遞磁盤工廠對象 ? ? ? ? ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); ? ? ? ? //獲取當前日期,文件夾名字使用 ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); ? ? ? ? String stringDate = sdf.format(new Date()); ? ? ? ? try { ? ? ? ? ? ? //解析對象的方法,parseRequest,解析reqeust對象 ? ? ? ? ? ? //返回集合 List, 集合的泛型,是另一個對象: 文件項對象 ? ? ? ? ? ? //文件項對象: 客戶端提交的任何數據,都認為是一個文件項 (普通文本框,附件上傳) ? ? ? ? ? ? //FileItem 客戶端的每一個上傳數據 (當前案例是7 個 文件項) ? ? ? ? ? ? List<FileItem> fileItemList = servletFileUpload.parseRequest(request); ? ? ? ? ? ? //遍歷集合,獲取每個文件項對象 ?fileItem ? ? ? ? ? ? for(FileItem fileItem : fileItemList){ ? ? ? ? ? ? ? ? //判斷 ?fileItem文件項對象方法,判斷出當前的文件項是普通文本,還是附件 ? ? ? ? ? ? ? ? if (fileItem.isFormField()){ //isFormField()返回true,普通項 , 返回false 是附件 ? ? ? ? ? ? ? ? ? ? //普通文本,取出用戶在文本框輸入的數據,帶編碼表名 ? ? ? ? ? ? ? ? ? ? String s = fileItem.getString("utf-8"); ? ? ? ? ? ? ? ? ? ? //方法: 獲取表單input的name的屬性值 ? ? ? ? ? ? ? ? ? ? String name = fileItem.getFieldName(); ? ? ? ? ? ? ? ? ? ?//System.out.println(name+"==="+s); ? ? ? ? ? ? ? ? ? ? //數據,封裝到Map集合 ? ? ? ? ? ? ? ? ? ? map.put(name,new String[]{s}); ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? //方法isFormField()返回false,是附件項 ? ? ? ? ? ? ? ? ? ? //FileItem對象方法 getName()獲取到上傳的文件名 ? ? ? ? ? ? ? ? ? ? String fileName = fileItem.getName(); ? ? ? ? ? ? ? ? ? ? //文件上傳,是需要修改文件名 ? ? ? ? ? ? ? ? ? ? //System.out.println(fileName); // Jellyfish.jpg ? ? ? ? ? ? ? ? ? ? //獲取文件的后綴名,獲取文件名 . 出現的索引 ? ? ? ? ? ? ? ? ? ? int index = ?fileName.lastIndexOf("."); ? ? ? ? ? ? ? ? ? ? //截取字符串 ? ? ? ? ? ? ? ? ? ? fileName = fileName.substring(index); ? ? ? ? ? ? ? ? ? ? //System.out.println(fileName); ? ? ? ? ? ? ? ? ? ? //自定義新的文件名 ? ? ? ? ? ? ? ? ? ? fileName = "itheima"+System.currentTimeMillis()+ UUIDUtil.getId()+fileName; ? ? ? ? ? ? ? ? ? ? //System.out.println(fileName); ? ? ? ? ? ? ? ? ? ? /** ? ? ? ? ? ? ? ? ? ? ?* 處理的上傳路徑,項目的目錄 E:\heima364\store\src\main\webapp\web\resources\products ? ? ? ? ? ? ? ? ? ? ?* 開發(fā)的項目: 跨平臺運行 ? ? ? ? ? ? ? ? ? ? ?* 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對象實現,路徑的合并 (上傳路徑path,和日期字符串合并為一個路徑) ? ? ? ? ? ? ? ? ? ? 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,組成一個新的File對象 ? ? ? ? ? ? ? ? ? ? //E:\heima364\store\src\main\webapp\web\resources\products\2020-02-27\itheima158279119698617ad226bf52d4eb4bc9cd97dbbd1fd5a.jpg ? ? ? ? ? ? ? ? ? ? File uploadDirFile = new File(uploadDir,fileName); ? ? ? ? ? ? ? ? ? ? //文件賦值,字節(jié)流讀取文件 ? ? ? ? ? ? ? ? ? ? //文件項對象的方法, getInputStream獲取輸入流,讀取的是上傳的文件 ? ? ? ? ? ? ? ? ? ? InputStream inputStream = fileItem.getInputStream(); ? ? ? ? ? ? ? ? ? ? //字節(jié)的輸出流 ? ? ? ? ? ? ? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(uploadDirFile); ? ? ? ? ? ? ? ? ? ? //commonsIO工具的方法來實現 ? ? ? ? ? ? ? ? ? ? IOUtils.copy(inputStream,fileOutputStream); ? ? ? ? ? ? ? ? ? ? fileOutputStream.close(); ? ? ? ? ? ? ? ? ? ? //從上傳的路徑中uploadDirFile,獲取出一部分路徑,寫入到數據庫 ? ? ? ? ? ? ? ? ? ? //File對象的方法toString(),路徑轉成字符串 ? ? ? ? ? ? ? ? ? ? //獲取resources出現的索引 ? ? ? ? ? ? ? ? ? ? index = uploadDirFile.toString().indexOf("resources"); ? ? ? ? ? ? ? ? ? ? String pimage = uploadDirFile.toString().substring(index); ? ? ? ? ? ? ? ? ? ? //替換路徑中的 / ? ? ? ? ? ? ? ? ? ? pimage =pimage.replace("\\","/"); ? ? ? ? ? ? ? ? ? ? //路徑,封裝到Map集合中/ ? ? ? ? ? ? ? ? ? ? map.put("pimage",new String[]{pimage}); ? ? ? ? ? ? ? ? ? ? fileItem.delete();//刪除上傳的臨時文件 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }catch (Exception ex){ ? ? ? ? ? ? ex.printStackTrace(); ? ? ? ? } ? ? ? ? //手動封裝Map中缺少的數據 ? ? ? ? //商品主鍵 ? ? ? ? map.put("pid",new String[]{UUIDUtil.getId()}); ? ? ? ? //上架,固定為0 ? ? ? ? map.put("pflag",new String[]{"0"}); ? ? ? ? //商品的發(fā)布日期 ? ? ? ? map.put("pdate",new String[]{stringDate}); ? ? ? ? return map; ? ? }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于MockMvc進行springboot調試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進行springboot調試(SpringbootTest),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10spring boot 自定義規(guī)則訪問獲取內部或者外部靜態(tài)資源圖片的方法
這篇文章主要介紹了spring boot 自定義規(guī)則訪問獲取內部或者外部靜態(tài)資源圖片的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Spring Cloud Config Client超時及重試示例詳解
這篇文章主要給大家介紹了關于Spring Cloud Config Client超時及重試的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2018-05-05SpringBoot實現固定和動態(tài)定時任務的三種方法
定時器是我們項目中經常會用到的,本文主要介紹了SpringBoot實現固定和動態(tài)定時任務的三種方法,具有一定的參考價值,感興趣的可以了解一下2023-09-09Resilience4J通過yml設置circuitBreaker的方法
Resilience4j是一個輕量級、易于使用的容錯庫,其靈感來自Netflix Hystrix,但專為Java 8和函數式編程設計,這篇文章主要介紹了Resilience4J通過yml設置circuitBreaker的方法,需要的朋友可以參考下2022-10-10