webuploader+springmvc實現圖片上傳功能
本文為大家分享了webuploader springmvc實現圖片上傳的具體代碼,供大家參考,具體內容如下
創新互聯成立以來不斷整合自身及行業資源、不斷突破觀念以使企業策略得到完善和成熟,建立了一套“以技術為基點,以客戶需求中心、市場為導向”的快速反應體系。對公司的主營項目,如中高端企業網站企劃 / 設計、行業 / 企業門戶設計推廣、行業門戶平臺運營、手機APP定制開發、手機網站制作設計、微信網站制作、軟件開發、IDC機房托管等實行標準化操作,讓客戶可以直觀的預知到從創新互聯可以獲得的服務效果。
jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here 圖片上傳
選擇文件
springMvc 的 servlet加入以下代碼(允許上傳):
引入的包
commons-io-1.3.2.jar
commons-fileupload-1.2.1.jar
java代碼
package com.shopping.controller; import java.io.File; import java.io.IOException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * @author MAZN * @date 創建時間:2017年5月2日 下午10:02:36 * @parameter * @return */ @Controller public class UploadImgController { int counter = 0; @RequestMapping("/uploader") public void upload(HttpServletRequest request,HttpServletResponse response){ //String fileName; // File tagetFile; System.out.println("收到圖片!"); MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request; Mapfiles = Murequest.getFileMap();//得到文件map對象 //String upaloadUrl = request.getSession().getServletContext().getRealPath("/")+"upload/";//得到當前工程路徑拼接上文件名 String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); int num=t.indexOf(".metadata"); String small = "small"; String upaloadUrl=t.substring(1,num).replace('/', '\\')+"image\\"+small+"\\"; //+"項目名\\WebContent\\文件"; File dir = new File(upaloadUrl); System.out.println(upaloadUrl); String img_url = upaloadUrl;//圖片路徑 if(!dir.exists())//目錄不存在則創建 dir.mkdirs(); for(MultipartFile file :files.values()){ counter++; String fileName=file.getOriginalFilename(); File tagetFile = new File(upaloadUrl+fileName);//創建文件對象 img_url += fileName; if(!tagetFile.exists()){//文件名不存在 則新建文件,并將文件復制到新建文件中 try { tagetFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { file.transferTo(tagetFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(img_url); System.out.println("接收完畢"+counter); } }
參考:WebUploader客戶端批量上傳圖片 后臺使用springMVC
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
分享文章:webuploader+springmvc實現圖片上傳功能
地址分享:http://www.xueling.net.cn/article/pojgpp.html