Baidu upload files editor

Object editor Baidu distal acceptable (distal required)

 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 (Note that the conversion request)

 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                  // returns to the original file on the client system, a file name 
10                  String fileRealName = file.getOriginalFilename ();
 . 11  
12 is                  int pointIndex = fileRealName.lastIndexOf ( "."); // position number of the dot 
13 is                  String fileSuffix = fileRealName.substring (pointIndex); // intercepting the file extension 
14  
15                  String UUID = UUID.randomUUID () toString ();.
 16  
. 17                  a Date D = new new a Date ();
 18 is                  the SimpleDateFormat formatYear = new new the 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     } 

File format conversion (MultipartFile turn 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     }

 

Guess you like

Origin www.cnblogs.com/zhanglingbing/p/10974870.html