當前位置:
首頁 > 知識 > 「SpringMVC」上傳並顯示圖片

「SpringMVC」上傳並顯示圖片

Spring配置文件上傳功能

基於Servelt的配置

在繼承了AbstractAnnotationConfigDispatcherServletInitializer的類中重寫customizeRegistration方法。

@Override

protected void customizeRegistration(ServletRegistration.Dynamic registration) {

// F:/tmp/是用於臨時存儲上傳文件的地方

registration.setMultipartConfig(new MultipartConfigElement("F:/tmp/"));

}

1

2

3

4

5

這裡只是一個比較簡單的例子,實際上MultipartConfigElement的構造器還能指定上傳文件的最大值,最大的請求值,文件的最小值。

單圖片上傳

上傳文件需要在表單添加enctype="multipart/form-data",一個基礎的單文件上傳頁面

<form action="/upload" method="post" enctype="multipart/form-data">

<input type="file" name="imageFile"/>

<button type="submit">提交</button>

</form>

1

2

3

4

FileUploadController

@Controller

public class FileUploadController {

@PostMapping("/upload")

public String uploadFile(@RequestParam("imageFile") MultipartFile imageFile, HttpSession session) throws IOException {

String fileName = imageFile.getOriginalFilename();

if (!imageFile.isEmpty()) {

String filePathPrefix = session.getServletContext().getRealPath("./");

imageFile.transferTo(new File(filePathPrefix, "/static/uploads/" + fileName));

session.setAttribute("imagepath", "/static/uploads/" + fileName);

return "redirect:/showimage";

}

return "upload";

}

@RequestMapping(value = "/upload", method = RequestMethod.GET)

public String getUploadForm() {

return "upload";

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

@PostMapping是@RequestMapping(..., method=RquestMethod.POST)的縮寫,表示處理的是post方法。@RequestParam註解用於綁定提交的參數,括弧里的imageFile和表單中是一樣的,這裡的參數類型為MultipartFile,表示上傳的是一個文件,在處理的時候相比byte流會比較方便。上面代碼還有很多問題比如沒有判斷重名文件,沒有檢查文件後綴,文件大小等問題,需要完善。

MultipartFile

getBytes() 獲取byte[]數組

getContentType() 獲取文件類型

getOriginalFilename() 獲取上傳文件的名字

getSize() 獲取文件大小

isEmpty() 文件是否為空

transferTo() 把文件轉存到指定的位置

注意點

這裡提到的是在使用__Intellij Idea__開發時遇到的問題:

如何獲取部署到本地伺服器上之後的項目路徑?:

String filePathPrefix = session.getServletContext().getRealPath("./");

在我的電腦上是"F:/tomcat/webapps/ROOT",intellij在部署時會將target文件夾下的文件放在這個位置

imageFile.transferTo(new File(filePathPrefix, "/static/uploads/" + fileName));

將文件轉存到F:/tomcat/webapps/ROOT/static/uploads/文件夾下。但實際操作的時候會發現出現找不到路徑的錯誤,而我已經在src/main/webapp下建立了static/uploads文件夾。經過在網上查資料,原來是需要自己手動在target文件夾下新建static/uploads文件夾,不然打包的時候伺服器下不會有相應的文件夾。

圖片顯示

在FileUploadController中可以看到,在session中設置了imagepath屬性,值為/static/uploads/filename,並重定向到/showimage

ImageShowController

@Controller

public class ImageShowController {

@RequestMapping("/showimage")

public String showImage(HttpServletRequest request, Model model, HttpSession session) {

String imagePath = (String) session.getAttribute("imagepath");

model.addAttribute("imagepath", imagePath);

return "uploaded";

}

}

1

2

3

4

5

6

7

8

9

10

uploaded.jsp

<body>

<img src="${imagepath}" alt="">

</body>

1

2

3

前端做的比較簡單,只需要src的值設置正確的路徑就行了

困惑

看到網上很多blog說springmvc在獲取靜態資源時需要添加

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/static/**").addResourceLocations("/static/");

}

1

2

3

4

或者

<mvc:resources location="/static/ mapping="/static/**"/>

1

這裡兩個星號是因為如果/static/下還有文件夾,只使用/static/*,不會匹配該文件夾里的內容

但實際操作的時候一個都沒添加也沒出現問題,不知道是不是我用的是Spring 5的關係…

「SpringMVC」上傳並顯示圖片

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

Eclipse編輯Spring配置文件xml時自動提示類class包名
tomcat 在liunx系統中shutdown後進程仍然存在解決辦法

TAG:程序員小新人學習 |