java-web實(shí)現(xiàn)文件的上傳

public String addProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        
            try {
                //1創(chuàng)建hashmap來接收頁面的數(shù)據(jù)
                HashMap<String, Object> map =new HashMap<>();
            
                //2創(chuàng)建文件項(xiàng)對(duì)象
                DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
        
                //3創(chuàng)建文件上傳對(duì)象
                ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);
                
                //4通過文件上傳對(duì)象解析所有數(shù)據(jù),用集合保存
                List<FileItem> filelist = fileUpload.parseRequest(request);
                
                //5通過foreach循環(huán)遍歷集合中的數(shù)據(jù)
                for (FileItem fItem : filelist) {
                    
                    //判斷是否是普通的上傳控件
                    if (fItem.isFormField()) {
                        
                        map.put(fItem.getFieldName(), fItem.getString("utf-8"));
                        
                    } else {
                        // 獲取上傳組件的名字
                        String name = fItem.getName();
                    
                        //為了文件安全,打亂文件的名字
                        String uuidName = UploadUtils.getUUIDName(name);
                        
                        //設(shè)置文件上傳的位置
                        String path = this.getServletContext().getRealPath("/img2/product/other");
                        
                        //輸入流
                        InputStream input = fItem.getInputStream();
                        
                        //輸出流
                        FileOutputStream output = new FileOutputStream(new File(path, uuidName));
                    
                        //對(duì)拷
                        IOUtils.copy(input, output);
                        
                        //關(guān)閉資源
                        input.close();
                        output.close();
                        
                        //將文件和文件路徑存儲(chǔ)到map集合里去
                        map.put(fItem.getFieldName(), "/img2/product/other/" + uuidName);
                    }
                }
                //封裝product數(shù)據(jù)
                Product product = new Product();
                BeanUtils.populate(product, map);
                
                //手動(dòng)設(shè)置product無法自己添加的數(shù)據(jù),
                product.setPid(UUIDUtils.getId());
                
                product.setPdate(new Date());
                
                Category c = new Category();
                
                c.setCid((String)map.get("cid"));
                
                product.setCategory(c);
                
                ps.addProduct(product);
                
                response.sendRedirect(request.getContextPath()+"/adminProduct?method=findAllProduct");
            } catch (Exception e) {
                e.printStackTrace();
            }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容