springMVC之檔案上傳,springmvc檔案上傳
1. 引入Jar包
commons-fileupload-1.2.2.jar
commons-io-2.1.jar
2.user-servlet.xml中設定檔上傳
<!-- 設定檔上傳 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="5000000"></property></bean>
3. UserController.java
@Controller@RequestMapping("/user")public class UserController {// 使用map類比資料庫 private Map<String, User> userMap = new HashMap<String, User>();public UserController() {userMap.put("zhangsan", new User("zhangsan", "123"));userMap.put("lishimin", new User("lishimin", "456"));}// 檔案上傳// 存取方法: http://localhost/springmvc_user/upload.jsp@RequestMapping(value="/upload",method=RequestMethod.POST)public String upload(@RequestParam("attachs")MultipartFile[] attachs,HttpServletRequest req) throws IOException {String realpath = req.getSession().getServletContext().getRealPath("/resources/upload");System.out.println(realpath);for(MultipartFile attach:attachs) {if(attach.isEmpty()){continue;}File file = new File(realpath + "/" + attach.getOriginalFilename()); FileUtils.copyInputStreamToFile(attach.getInputStream(),file);}return "success";}}4. upload.jsp
<%@ 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="text/html; charset=UTF-8"><title>檔案上傳</title></head><body><form method="post" action="user/upload" enctype="multipart/form-data">Attach:<br/> <input type="file" name="attachs"/><br/> <input type="file" name="attachs"/><br/> <input type="file" name="attachs"/><br/> <input type="submit" value="上傳檔案"/></form></body></html>
spring MVC 上傳檔案問題
kudaaps 的回答暈死
他說你參數錯了,問題不大,你把imgsrc改成string型的呢!
++++++++++++++++++++++++++++++++++++++++++++++
我看他的意思好像說他要type [[B]但是你給了java.lang.String。。
我對這個不熟,spring也沒用過。。。
你說io.File要byte[]我也不知道。。。
我看文檔倒是string....
java.sun.com/...l#File(java.io.File,%20java.lang.String)
spring mvc 怎實現不用表單上傳檔案
前台js用jquery等架構,jquery有uploadify等上傳檔案外掛程式