SpringMVC多個文件上傳及上傳后立即顯示圖片功能
多文件上傳就是改良一個方法把MultipartFile類換成CommonsMultipartFile類,因為上傳多個文件用數(shù)組方式的話MultipartFile類不能初始化,它不支持數(shù)組
package com.meng.upload; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; @Controller public class upload { @RequestMapping(value="/upload",method=RequestMethod.POST) public String upload(@RequestParam("file1") CommonsMultipartFile[] file1, HttpServletRequest request) { for (CommonsMultipartFile commonsMultipartFile : file1) { try { File file = new File(request.getServletContext().getRealPath("upload"), System.currentTimeMillis() + "_" + commonsMultipartFile.getOriginalFilename()); System.out.println(file.getPath()+""); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(commonsMultipartFile.getBytes()); fileOutputStream.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "ok"; } }
index.jsp頁面上傳立即顯示圖片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> <script type="text/javascript"> //判斷瀏覽器是否支持FileReader接口 if (typeof FileReader == 'undefined') { alert("<h1>當前瀏覽器不支持FileReader接口</h1>"); } //選擇圖片,馬上預(yù)覽 function xmTanUploadImg(obj) { var file = obj.files[0]; var reader = new FileReader(); reader.onload = function(e) { var img = document.getElementById("img1"); img.src = e.target.result; } reader.readAsDataURL(file); } </script> </head> <body> <form action="upload.upload" method="post" enctype="multipart/form-data"> file1::<input type="file" name="file1" accept=".jpg,.png" onchange="xmTanUploadImg(this)" /> <input type="submit" /> <img id="img1" /> </form> </body> </html>
總結(jié)
以上所述是小編給大家介紹的SpringMVC多個文件上傳及上傳后立即顯示圖片功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Mybatis實戰(zhàn)之TypeHandler高級進階
本文主要介紹了自定義的枚舉TypeHandler的相關(guān)知識,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片
這篇文章主要為大家詳細介紹了利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現(xiàn)數(shù)據(jù)庫訪問功能
這篇文章主要介紹了SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現(xiàn)數(shù)據(jù)庫訪問功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Java獲取項目路徑方式System.getProperty(“user.dir“)
這篇文章主要介紹了Java獲取項目路徑方式System.getProperty(“user.dir“),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Spring Boot實現(xiàn)qq郵箱驗證碼注冊和登錄驗證功能
這篇文章主要給大家介紹了關(guān)于Spring Boot實現(xiàn)qq郵箱驗證碼注冊和登錄驗證功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12