百度编辑器上传文件
百度编辑器前端接受的对象(前端要求的)
1 public class ImageVo implements Serializable { 2 private static final long serialVersionUID = -3699242044954063640L; 3 4 private String original = ""; 5 private int size = 0; 6 private String state = "ERROR"; 7 private String title = ""; 8 private String type = ""; 9 private String url = ""; 10 }
controller(需要注意的是请求的转换)
1 public ImageVo init(HttpServletRequest request, HttpServletResponse response) { 2 try { 3 CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver( 4 request.getSession().getServletContext()); 5 if(multipartResolver.isMultipart(request)){ 6 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request; 7 Map<String, MultipartFile> fileMap = multiRequest.getFileMap(); 8 MultipartFile file = fileMap.get("upfile"); 9 //返回原来在客户端的文件系统的文件名 10 String fileRealName = file.getOriginalFilename(); 11 12 int pointIndex = fileRealName.lastIndexOf(".");//点号的位置 13 String fileSuffix = fileRealName.substring(pointIndex);//截取文件后缀 14 15 String uuid = UUID.randomUUID().toString(); 16 17 Date d = new Date(); 18 SimpleDateFormat formatYear = new SimpleDateFormat("yyyy"); 19 SimpleDateFormat formatMonthDay = new SimpleDateFormat("MMdd"); 20 String year = formatYear.format(d); 21 String monthDay = formatMonthDay.format(d); 22 String imgStr = "/" + "img" + "/" + year + "/" + monthDay + "/" + uuid + fileSuffix; 23 24 ImageVo imageVo = new ImageVo(); 25 File toFile = multipartFileToFile(file); 26 if (toFile == null){ 27 return new ImageVo(); 28 } 29 UpYun upYun = new UpYun(); 30 try { 31 // 图片上传到upyun 32 boolean writeFile = upYun.writeFile(imgStr, toFile); 33 if (writeFile) { 34 imageVo.setOriginal(fileRealName); 35 imageVo.setState("SUCCESS"); 36 imageVo.setType(fileSuffix); 37 imageVo.setTitle(uuid + fileSuffix); 38 String url = "http://img.hotyq.com" + imgStr; 39 imageVo.setUrl(url); 40 return imageVo; 41 } else { 42 log.error("TDCode--generateTDCodeImage--upyun upload fail"); 43 return new ImageVo(); 44 } 45 } catch (IOException e) { 46 log.error("TDCode--generateTDCodeImage--upyun upload fail:", e); 47 return new ImageVo(); 48 } finally { 49 toFile.delete(); 50 } 51 }else { 52 return new ImageVo(); 53 } 54 55 } catch (Exception e) { 56 // TODO Auto-generated catch block 57 log.error("admin--UploadController-init--", e); 58 return new ImageVo(); 59 } 60 }
文件格式转换(MultipartFile转file)
1 public static File multipartFileToFile(@RequestParam MultipartFile file) throws Exception { 2 File toFile = null; 3 if (file.equals("") || file.getSize() <= 0) { 4 file = null; 5 return null; 6 } else { 7 InputStream ins = null; 8 ins = file.getInputStream(); 9 toFile = new File(file.getOriginalFilename()); 10 inputStreamToFile(ins, toFile); 11 ins.close(); 12 return toFile; 13 } 14 } 15 16 public static void inputStreamToFile(InputStream ins, File file) { 17 try { 18 OutputStream os = new FileOutputStream(file); 19 int bytesRead = 0; 20 byte[] buffer = new byte[8192]; 21 while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { 22 os.write(buffer, 0, bytesRead); 23 } 24 os.close(); 25 ins.close(); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 }