commons fileupload實(shí)現(xiàn)文件上傳的實(shí)例代碼
一、文件上傳的原理分析
1、文件上傳的必要前提
a、表單的method必須是post
b、表單的enctype屬性必須是multipart/form-data類型的。
enctype默認(rèn)值:application/x-www-form-urlencoded
作用:告知服務(wù)器,請(qǐng)求正文的MIME類型
application/x-www-form-urlencoded : username=abc&password=123
ServletRequest.getParameter(String name);該方法是專門讀取該類型的方法
multipart/form-data:

2、借助commons-fileupload組件實(shí)現(xiàn)文件的上傳
a、拷貝jar包:commons-fileupload.jar commons-io.jar
b、實(shí)現(xiàn)原理

3、亂碼問題
a、普通字段的亂碼
FileItem.getString(String charset);編碼要和客戶端一致
b、上傳的中文文件名亂碼
request.setCharacterEncoding("UTF-8");編碼要和客戶端一致
4、具體實(shí)現(xiàn)
前臺(tái)upload.jsp代碼如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上傳</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/servlet/UploadServlet3" method="post" enctype="multipart/form-data">
name:<input name="name"/><br/>
file1:<input type="file" name="f1"/><br/>
file2:<input type="file" name="f2"/><br/>
<input type="submit" value="上傳">
</form>
</body>
</html>
后臺(tái)servlet代碼
package com.itheima.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
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.FileUploadBase;
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.FilenameUtils;
//詳解
public class UploadServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.print(request.getRemoteAddr());
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
throw new RuntimeException("請(qǐng)檢查您的表單的enctype屬性,確定是multipart/form-data");
}
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload parser = new ServletFileUpload(dfif);
// parser.setFileSizeMax(3*1024*1024);//設(shè)置單個(gè)文件上傳的大小
// parser.setSizeMax(6*1024*1024);//多文件上傳時(shí)總大小限制
List<FileItem> items = null;
try {
items = parser.parseRequest(request);
}catch(FileUploadBase.FileSizeLimitExceededException e) {
out.write("上傳文件超出了3M");
return;
}catch(FileUploadBase.SizeLimitExceededException e){
out.write("總文件超出了6M");
return;
}catch (FileUploadException e) {
e.printStackTrace();
throw new RuntimeException("解析上傳內(nèi)容失敗,請(qǐng)重新試一下");
}
//處理請(qǐng)求內(nèi)容
if(items!=null){
for(FileItem item:items){
if(item.isFormField()){
processFormField(item);
}else{
processUploadField(item);
}
}
}
out.write("上傳成功!");
}
private void processUploadField(FileItem item) {
try {
String fileName = item.getName();
//用戶沒有選擇上傳文件時(shí)
if(fileName!=null&&!fileName.equals("")){
fileName = UUID.randomUUID().toString()+"_"+FilenameUtils.getName(fileName);
//擴(kuò)展名
String extension = FilenameUtils.getExtension(fileName);
//MIME類型
String contentType = item.getContentType();
if(contentType.startsWith("image/")){
//分目錄存儲(chǔ):日期解決
// Date now = new Date();
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//
// String childDirectory = df.format(now);
//按照文件名的hashCode計(jì)算存儲(chǔ)目錄
String childDirectory = makeChildDirectory(getServletContext().getRealPath("/WEB-INF/files/"),fileName);
String storeDirectoryPath = getServletContext().getRealPath("/WEB-INF/files/"+childDirectory);
File storeDirectory = new File(storeDirectoryPath);
if(!storeDirectory.exists()){
storeDirectory.mkdirs();
}
System.out.println(fileName);
item.write(new File(storeDirectoryPath+File.separator+fileName));//刪除臨時(shí)文件
}
}
} catch (Exception e) {
throw new RuntimeException("上傳失敗,請(qǐng)重試");
}
}
//計(jì)算存放的子目錄
private String makeChildDirectory(String realPath, String fileName) {
int hashCode = fileName.hashCode();
int dir1 = hashCode&0xf;// 取1~4位
int dir2 = (hashCode&0xf0)>>4;//取5~8位
String directory = ""+dir1+File.separator+dir2;
File file = new File(realPath,directory);
if(!file.exists())
file.mkdirs();
return directory;
}
private void processFormField(FileItem item) {
String fieldName = item.getFieldName();//字段名
String fieldValue;
try {
fieldValue = item.getString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("不支持UTF-8編碼");
}
System.out.println(fieldName+"="+fieldValue);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
5、關(guān)于臨時(shí)文件問題
a、DiskFileItemFactory
public void setRepository(File repository):設(shè)置臨時(shí)文件的存放目錄
public void setSizeThreshold(int sizeThreshold):設(shè)置緩存的大小
b、
文件上傳時(shí),自己用IO流處理,一定要在流關(guān)閉后刪除臨時(shí)文件。FileItem.delete()
建議使用:FileItem.writer(File f).會(huì)自動(dòng)刪除臨時(shí)文件。
6、限制文件的大小
a、
ServletFileUpload.setFileSizeMax(3*1024*1024);//設(shè)置單個(gè)文件上傳的大小
b、
ServletFileUpload.setSizeMax(6*1024*1024);//多文件上傳時(shí)總大小限制
以上所述是小編給大家介紹的commons fileupload實(shí)現(xiàn)文件上傳的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Apache commons fileupload文件上傳實(shí)例講解
- java組件commons-fileupload實(shí)現(xiàn)文件上傳、下載、在線打開
- Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
- JavaEE組件commons-fileupload實(shí)現(xiàn)文件上傳、下載
- JSP組件commons-fileupload實(shí)現(xiàn)文件上傳
- java組件commons-fileupload文件上傳示例
- Apache Commons fileUpload文件上傳多個(gè)示例分享
- java組件commons-fileupload實(shí)現(xiàn)文件上傳
- java組件fileupload文件上傳demo
- java使用common-fileupload實(shí)現(xiàn)文件上傳
相關(guān)文章
idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決
這篇文章主要介紹了idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
本文主要介紹了SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Java之判斷2000~2023年有哪些年份是閏年并打印輸出
這篇文章主要介紹了Java之判斷2000~2023年有哪些年份是閏年并打印輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
解決Mybatis返回update后影響的行數(shù)問題
這篇文章主要介紹了解決Mybatis返回update后影響的行數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
關(guān)于后綴表達(dá)式的java實(shí)現(xiàn)過程
這篇文章主要介紹了關(guān)于后綴表達(dá)式的java實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
詳解IDEA中類加載器調(diào)用getResourceAsStream()方法需注意的問題
這篇文章主要介紹了詳解IDEA中類加載器調(diào)用getResourceAsStream()方法需注意的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

