第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

上傳文件問題?

上傳文件問題?

慕姐8946757 2017-09-27 23:03:35
package com.itheima.web.servlet;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;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.tomcat.util.http.fileupload.IOUtils;import org.fujb.commons.CommonsUtil;@WebServlet("/AddFileServlet")public class AddFileServlet extends HttpServlet {?? ?private static final long serialVersionUID = 1L;?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response)?? ??? ??? ?throws ServletException, IOException {?? ??? ?doPost(request, response);?? ?}?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response)?? ??? ??? ?throws ServletException, IOException {?? ??? ?// 使用FileUpload完成文件上傳?? ??? ?/**?? ??? ? * 使用Commons-FileUpload完成文件上傳的步驟: * 1、生成工廠類 DiskFileItemFactory *?? ??? ? * 2、生成解析器類 ServletFileUpload * 3、解析request對(duì)象,的到表單項(xiàng)的對(duì)象的集合List?? ??? ? * <FileItem> * 4、遍歷list集合,獲取相關(guān)表單項(xiàng)的值?? ??? ? */?? ??? ?// 創(chuàng)建工廠類?? ??? ?DiskFileItemFactory factory = new DiskFileItemFactory();?? ??? ?// 設(shè)置文件的緩存路徑?? ??? ?factory.setRepository(new File("D:/upload"));?? ??? ?// 設(shè)置上傳的文件大于多少的時(shí)候需要緩存,即大小限制?? ??? ?factory.setSizeThreshold(1024 * 1024 * 3);?? ??? ?// 創(chuàng)建解析器類,需要工廠類作為參數(shù)?? ??? ?ServletFileUpload fileUpload = new ServletFileUpload(factory);?? ??? ?// 如果文件名出現(xiàn)中文,并且中文亂碼,就可以通過一下設(shè)置規(guī)避?? ??? ?fileUpload.setHeaderEncoding("UTF-8");?? ??? ?try {?? ??? ??? ?// 解析request,得到表單項(xiàng)對(duì)象的集合?? ??? ??? ?List<FileItem> list = fileUpload.parseRequest(request);?? ??? ??? ?for (FileItem fileItem : list) {?? ??? ??? ??? ?if (fileItem.isFormField()) {?? ??? ??? ??? ??? ?// 是普通表單項(xiàng)?? ??? ??? ??? ??? ?// 獲取普通項(xiàng)的值?? ??? ??? ??? ??? ?// String value = fileItem.getString();?? ??? ??? ??? ??? ?// 對(duì)普通項(xiàng)中文亂碼的處理?? ??? ??? ??? ??? ?String value = fileItem.getString("UTF-8");?? ??? ??? ??? ??? ?// 獲取當(dāng)前表單項(xiàng)的name的值?? ??? ??? ??? ??? ?String fieldName = fileItem.getFieldName();?? ??? ??? ??? ??? ?System.out.println("表單項(xiàng)name : " + fieldName + ", 值是: " + value);?? ??? ??? ??? ?} else {?? ??? ??? ??? ??? ?// 是文件表單項(xiàng)?? ??? ??? ??? ??? ?// 獲取文件名?? ??? ??? ??? ??? ?String fileName = fileItem.getName();?? ??? ??? ??? ??? ?System.out.println("文件名: " + fileName);?? ??? ??? ??? ??? ?// 獲取唯一文件名?? ??? ??? ??? ??? ?fileName = CommonsUtil.UUID() + "_" + fileName;?? ??? ??? ??? ??? ?// 目錄分離?? ??? ??? ??? ??? ?String hexString = Integer.toHexString(fileName.hashCode());?? ??? ??? ??? ??? ?String path = hexString.charAt(0) + "/" + hexString.charAt(1);?? ??? ??? ??? ??? ?File destFile = new File("D:/upload/" + path);?? ??? ??? ??? ??? ?destFile.mkdirs();?? ??? ??? ??? ??? ?File file = new File(destFile, fileName);?? ??? ??? ??? ??? ?OutputStream out = new FileOutputStream(file);?? ??? ??? ??? ??? ?// 文件流?? ??? ??? ??? ??? ?InputStream in = fileItem.getInputStream();?? ??? ??? ??? ??? ?// 拷貝文件流?? ??? ??? ??? ??? ?IOUtils.copy(in, out);?? ??? ??? ??? ??? ?// 釋放資源?? ??? ??? ??? ??? ?IOUtils.closeQuietly(in);?? ??? ??? ??? ??? ?IOUtils.closeQuietly(out);?? ??? ??? ??? ?}?? ??? ??? ?}?? ??? ?} catch (FileUploadException e) {?? ??? ??? ?e.printStackTrace();?? ??? ?}?? ?}}? // 釋放資源?? ??? ??? ??? ??? ?IOUtils.closeQuietly(in);?? ??? ??? ??? ??? ?IOUtils.closeQuietly(out);這個(gè)位置顯示錯(cuò)誤,說安靜地關(guān)閉未定義怎么解決?(The method closeQuietly(OutputStream) is undefined for the type IOUtils)
查看完整描述

1 回答

已采納
?
HansonQ

TA貢獻(xiàn)223條經(jīng)驗(yàn) 獲得超56個(gè)贊

對(duì)比一下啊jar包版本。這個(gè)提示是方法未定義

查看完整回答
反對(duì) 回復(fù) 2017-09-28
  • 1 回答
  • 0 關(guān)注
  • 1284 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)