上傳圖片、文件等,及文件夾
一、上傳圖片,文件(ajax上傳,並返回提示信息)
前端界面(jsp,css,jquery等)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
<script type="text/javascript">
function upload() {
var uploadfile= $("#uploadfile")[0].files[0];
var formData = new FormData();
formData.append('uploadfile',uploadfile);
$.ajax({
async: false,
url:'uploadadd.do',
type:'post',
data:formData,
processData:false,
contentType:false,
success:function (data) {
alert(data);
}, error:function () {
alert("失敗");
}
});
}
</script>
</head>
<body>
<form class="tabson" enctype="multipart/form-data">
文件:<input type="file" name="uploadfile" id="uploadfile" >
<input type="button" onclick="upload()" value="上傳文件">
</form>
</body>
</html>
控制層
@ResponseBody
@RequestMapping(value="uploadadd.do",method=RequestMethod.POST)
public String addAnnouncement(HttpServletRequest request,
@RequestParam("uploadfile") MultipartFile uploadfile) {
String name=uploadfile.getOriginalFilename();
String suffix = name.substring(name.lastIndexOf(".") + 1);
String localPath = request.getSession().getServletContext().getRealPath("/") + "data/Img_1";
System.out.println("商品存儲路徑:" + localPath);
// 創建圖片名稱
String filename = Uploadutil.getNowDate() + "." + suffix;
System.out.println(filename+"----------");
// 創建目錄 文件夾爲空就創建
File dir = new File(localPath);
if (!dir.exists()) {
//System.out.println("創建的文件夾爲" + dir);
dir.mkdirs();
}
// 圖片絕對路徑,拼接圖片路徑
String realPath = dir +"/"+ filename;
String realPath1=realPath.replace("/", "\\");
System.out.println(realPath1.toString()+"---------------------------------");
// 寫入圖片 將圖片保存在服務器下
File newFile = new File(realPath1);
try {
uploadfile.transferTo(newFile);
}catch( Exception e) {
e.printStackTrace();
}
System.out.println("request.getContextPath(): "+request.getContextPath());
// 拼接傳輸協議+域名+端口號+
String host = img_url + request.getContextPath();
// 數據庫存儲圖片絕對路徑
String sqlPath = host + "/data/Img/" + filename;
System.out.println("數據庫存儲的路徑:" + sqlPath);
return "成功";
}
結果展示
1、界面返回成功狀態
2、控制檯打印的信息
二、上傳excel 並解析excel內容
前端界面(jsp,css,jquery等)
<form action="uploadofexcel_1.do" enctype="multipart/form-data" method="post">
文件: <input type="file" name = "uploadFile" /><br>
<input type = "submit" value = "提交">
</form>
控制層
@RequestMapping(value = "uploadofexcel_1.do", produces = "application/text; charset=utf-8")
public String uploadofexcel_1(HttpServletRequest request,@RequestParam("uploadFile") MultipartFile uploadFile) {
if (uploadFile.isEmpty()) {
try {
throw new Exception("文件不存在!");
} catch (Exception e) {
e.printStackTrace();
}
}
InputStream in = null;
try {
in = uploadFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
List<List<Object>> listob = null;
try {
listob = new ExcelUtil().getBankListByExcel(in, uploadFile.getOriginalFilename());
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < listob.size(); i++) {
List<Object> lg=listob.get(i);
System.out.println(lg);
}
System.out.println( uploadFile.getOriginalFilename());
return "redirect:ShowUpload.do";
}
結果展示(解析excel的結果)
1、excel裏的信息,解析的內容爲List< List< Object>>形式,可將其轉換爲Lst< Object>形式。
三、上傳文件夾
前端界面(jsp,css,jquery等)
<form action="upload3.do" enctype="multipart/form-data" method="post">
圖片: <input type="file" name = "pictureFile" webkitdirectory/><br>
<input type = "submit" value = "提交">
</form>
控制層
@RequestMapping(value="upload3.do",method=RequestMethod.POST)
public String insertpicture3(HttpServletRequest request,
@RequestParam("pictureFile") MultipartFile[] pictureFiles){
List<Picture> list=new ArrayList<Picture>();
int i=1;
for (MultipartFile mf : pictureFiles) {
if(!mf.isEmpty()) {
// 獲取文件名
String name = mf.getOriginalFilename();
System.out.println(name);
// 獲取文件後綴
String suffix = name.substring(name.lastIndexOf(".") + 1);
System.out.println("1-----"+suffix);
/* if (!suffix.equalsIgnoreCase("png") && !suffix.equalsIgnoreCase("gif") && !suffix.equalsIgnoreCase("jpg")
&& !suffix.equalsIgnoreCase("jpeg") && !suffix.equalsIgnoreCase("bmp")) {
return "文件類型錯誤,請上傳圖片文件";
} */
String localPath = request.getSession().getServletContext().getRealPath("/") + "data/Img";
System.out.println("商品存儲路徑:" + localPath);
// 創建圖片名稱
String filename = Uploadutil.getNowDate() +"-"+i+ "." + suffix;
i++;
System.out.println(filename+"----------");
// 創建目錄 文件夾爲空就創建
File dir = new File(localPath);
if (!dir.exists()) {
//System.out.println("創建的文件夾爲" + dir);
dir.mkdirs();
}
// 圖片絕對路徑,拼接圖片路徑
String realPath = dir +"/"+ filename;
String realPath1=realPath.replace("/", "\\");
System.out.println(realPath1.toString()+"---------------------------------");
// 寫入圖片 將圖片保存在服務器下
File newFile = new File(realPath1);
try {
mf.transferTo(newFile);
}catch( Exception e) {
e.printStackTrace();
}
System.out.println("request.getContextPath(): "+request.getContextPath());
// 拼接傳輸協議+域名+端口號+
String host = img_url + request.getContextPath();
// 數據庫存儲圖片絕對路徑
String sqlPath = host + "/data/Img/" + filename;
System.out.println("數據庫存儲的路徑:" + sqlPath);
Picture pc=new Picture();
pc.setPname(filename);
pc.setUrls(sqlPath);
list.add(pc);
}
}
for (Picture picture : list) {
System.out.println(picture.toString());
}
//批量插入圖片信息
int num=uploadserviceimpl.InsertOfPicture(list);
System.out.println(num);
return "redirect:ShowUpload.do";
}
上傳的界面及結果
1、上傳文件夾的界面,webkitdirectory指向的是文件夾,若不帶webkitdirectory則指向的是根目錄,各種文件
/2、Chrome瀏覽器帶的功能,顯示該文件夾下有多少個子文件
3、文件上傳成功後顯示的結果(原先有兩個圖片,此次上傳的爲4個文件)
4、控制器打印的結果,上傳文件夾的信息