
文章插圖
這些天忙著刷題 , 又怕遺忘了spring boot , 所以抽出一點(diǎn)時(shí)間折騰折騰 , 加深點(diǎn)印象 。
spring boot 的文件上傳與 spring mvc 的文件上傳基本一致 , 只需注意一些配置即可 。
環(huán)境要求: Spring Boot v1.5.1.RELEASE + jdk1.7 + myeclipse
1).引入thymeleaf , 支持頁面跳轉(zhuǎn)
<!– 添加thymeleaf –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
123452).在 src/main/resources 目錄下新建 static 目錄和 templates 目錄 。static存放靜態(tài)文件 , 比如 css、js、image… templates 存放靜態(tài)頁面 。先在templates 中新建一個(gè) uploadimg.html
<!DOCTYPE html>
<html>
<head>
<title>uploadimg.html</title>
<meta name=”keywords” content=”keyword1,keyword2,keyword3″></meta>
<meta name=”description” content=”this is my page”></meta>
<meta name=”content-type” content=”text/html; charset=UTF-8″></meta>
<!–<link rel=”stylesheet” type=”text/css” href=https://www.520longzhigu.com/diannao/”./styles.css”>–>
</head>
<body>
<form enctype=”multipart/form-data” method=”post” action=”/testuploadimg”>
圖片<input type=”file” name=”file”/>
<input type=”submit” value=https://www.520longzhigu.com/diannao/”上傳”/>
</form>
</body>
</html>
12345678910111213141516171819203).在 controller 中寫兩個(gè)方法 , 一個(gè)方法跳轉(zhuǎn)到上傳文件的頁面 , 一個(gè)方法處理上傳文件
//跳轉(zhuǎn)到上傳文件的頁面
@RequestMapping(value=https://www.520longzhigu.com/diannao/”/gouploadimg”, method = RequestMethod.GET)
public String goUploadImg() {
//跳轉(zhuǎn)到 templates 目錄下的 uploadimg.html
return “uploadimg”;
}
//處理文件上傳
@RequestMapping(value=https://www.520longzhigu.com/diannao/”/testuploadimg”, method = RequestMethod.POST)
public @ResponseBody String uploadImg(@RequestParam(“file”) MultipartFile file,
HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
/*System.out.println(“fileName–>” + fileName);
System.out.println(“getContentType–>” + contentType);*/
String filePath = request.getSession().getServletContext().getRealPath(“imgupload/”);
try {
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
// TODO: handle exception
}
//返回json
return “uploadimg success”;
}
1234567891011121314151617181920212223244).在上面中 , 我將文件上傳的實(shí)現(xiàn)寫在工具類 FileUtil 的 uploadFile 方法中
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
123456789105).在瀏覽器輸入 :http://localhost:8080/gouploadimg 測試
上傳文件后:
在應(yīng)用的 src/main/webapp/imgupload 目錄下
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時(shí)就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- 海棠書屋:母親目睹父女被火吞沒!杭州電瓶車爆燃女孩燒傷95%或終身插管
- 海棠書屋:杭州電車燒傷女孩或終生插管!為什么不公布電動(dòng)車品牌?
- 杭州電瓶車行駛中爆燃,燒傷女孩琪琪病危或終生插管 伏天氏新章節(jié)
- 杭州電瓶車燒傷女孩或終生插管 伏天氏新章節(jié)
- 分享fileupload獲取文件路徑 fileupload控件上傳文章
- 手機(jī)txt文件打開亂碼修復(fù)方法 txt文檔亂碼怎么修復(fù)
- Excel文件轉(zhuǎn)Dat文件教程 dat是什么文件
- QQ閃照怎么保存221 閃圖怎么保存
- “抖音崩了”登上微博超話 或與被刪數(shù)據(jù)庫有關(guān)?
- 直播帶貨時(shí)微笑或表現(xiàn)出其他積極的情緒,會(huì)帶來反效果。
