小 小诗仙小白 2016-11-18 19:11 @RequestMapping(value = "upload", method = RequestMethod.POST) public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception { String localPath = "/static/upload"; String uploadPath = request.getRealPath(localPath); File dir = new File(uploadPath); if (!dir.exists()) dir.mkdirs(); String fileName = uploadPath + "/" + SequenceUtil.uuid2() + ".jpg"; if (request instanceof MultipartRequest) { MultiValueMap < String, MultipartFile > map = ((MultipartRequest) request).getMultiFileMap(); LinkedList < CommonsMultipartFile > imageList = (LinkedList) map.get("imagefile"); if (imageList != null && imageList.size() > 0) { CommonsMultipartFile file = imageList.get(0); if (!file.isEmpty()) { try { BASE64Encoder encoder = new BASE64Encoder(); BASE64Decoder decoder = new BASE64Decoder(); // 通过base64来转化图片 String data = encoder.encode(file.getBytes()); byte[] bytes = decoder.decodeBuffer(data); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { // 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(fileName); out.write(bytes); out.flush(); out.close(); } catch(Exception e) { e.printStackTrace(); } } } } return null; } o o咄咄o 2016-12-21 17:12 这是啥
简 简~单~ 2016-07-18 09:07 这个不错啊 . ... 2016-08-18 08:08 确实挺好用的 驭 驭势 2017-04-26 16:32 你们可以直接运行吗?怎么调试的,为甚么我的不能运行呢?
@RequestMapping(value = "upload", method = RequestMethod.POST) public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception { String localPath = "/static/upload"; String uploadPath = request.getRealPath(localPath); File dir = new File(uploadPath); if (!dir.exists()) dir.mkdirs(); String fileName = uploadPath + "/" + SequenceUtil.uuid2() + ".jpg"; if (request instanceof MultipartRequest) { MultiValueMap < String, MultipartFile > map = ((MultipartRequest) request).getMultiFileMap(); LinkedList < CommonsMultipartFile > imageList = (LinkedList) map.get("imagefile"); if (imageList != null && imageList.size() > 0) { CommonsMultipartFile file = imageList.get(0); if (!file.isEmpty()) { try { BASE64Encoder encoder = new BASE64Encoder(); BASE64Decoder decoder = new BASE64Decoder(); // 通过base64来转化图片 String data = encoder.encode(file.getBytes()); byte[] bytes = decoder.decodeBuffer(data); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { // 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(fileName); out.write(bytes); out.flush(); out.close(); } catch(Exception e) { e.printStackTrace(); } } } } return null; }