1、接收到的是圖片的流時
//上傳頭像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod.POST) @ResponseBody public String uploadHeadSculpture(@RequestParam("photo") String file) { User user = (User) SecurityUtils.getSubject().getSession().getAttribute("curr_user"); //獲取文件格式 String postfix = file.split("/")[1].split(";")[0]; //獲取圖片的Base64碼 String str = file.split(",")[1]; String url = ""; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解碼 byte[] bytes = decoder.decodeBuffer(str); for (int i = 0; i < bytes.length; ++i) { // 調整異常數據 if (bytes[i] < 0) { bytes[i] += 256; } } long title = Calendar.getInstance().getTimeInMillis(); //獲取系統路徑並設定文件保存的目錄 String dir = ServiceConfigUtil.getValue("imgpath");//圖片的上傳路徑,我這里是從工程的配置文件獲取的 String fileName = title + "." + postfix; // 生成jpeg圖片 FileUtils.writeByteArrayToFile(new File(dir, fileName), bytes); String lookUserPhoto = ServiceConfigUtil.getValue("lookUserPhoto");//圖片的訪問路徑,我這里是從工程配置文件獲取的,可以自己定義。如果你的圖片保存在工程目錄下,可以直接用dir+fileName url = lookUserPhoto + fileName;//保存到數據庫的圖片訪問路徑 /××
×保存url到數據庫
××/
} catch (Exception e) {return "no"; }return "yes"; }
注:接收參數file值的一個基本格式
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAdiUlEQVR........."
2、接收到的是file文件直接上傳
@RequestMapping(value={"/saveOrUpdate"},method=RequestMethod.POST) public String saveOrUpdate(Person p, @RequestParam("photo") MultipartFile file, HttpServletRequest request) throws IOException{ if(!file.isEmpty()){ ServletContext sc = request.getSession().getServletContext(); String dir = sc.getRealPath(“/upload”); //設定文件保存的目錄 String filename = file.getOriginalFilename(); //得到上傳時的文件名 FileUtils.writeByteArrayToFile(new File(dir,filename), file.getBytes()); p.setPhotoPath(“/upload/”+filename); //設置圖片所在路徑 System.out.println("upload over. "+ filename); } ps.saveOrUpdate(p); return "redirect:/person/list.action"; //重定向 }
2.1、頁面
<form action="/saveOrUpdate" enctype="multipart/form-data" method="post"> <input type="file" name="photo"> <input type="submit" value="commit"> </form>
2.2、需要在springmvc配置文件中添加
<!--上傳文件-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000000000"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
3、文件下載
private void downFile(String filename, String realPath, HttpServletResponse response) throws IOException { try { filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { log.warn("文件轉碼出現異常異常信息為:" + ExceptionUtil.print(e)); } log.debug("下載的文件名:" + filename); log.debug("下載的文件路徑:" + realPath); OutputStream os = null; InputStream inputStream = null; try { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + filename); inputStream = new FileInputStream(realPath); os = response.getOutputStream(); byte[] b = new byte[1024]; int length; while ((length = inputStream.read(b)) != -1) { os.write(b, 0, length); } os.flush(); } catch (IOException e) { log.warn("下載文件出現異常,異常信息為:" + ExceptionUtil.print(e)); } finally { inputStream.close(); os.close(); } }