zoukankan      html  css  js  c++  java
  • 百度编辑器上传文件

    百度编辑器前端接受的对象(前端要求的)

     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     }
  • 相关阅读:
    我为何看到你的提问不想回答?关于如何提问的一些看法
    零基础一步一步pytorch实现Logistic regression逻辑回归编程教程
    numpy与pytorch实现梯度下降,实现一个简单的两层神经网络
    [ubuntu]Gedit修改文件后提示无法创建备份文件同时不能保存修改过后的文件
    【测绘】高速公路中线放样
    【GDAL】图像处理三:图像平滑(一)
    【GDAL】图像处理二:初级图像读取,操作,存储。
    【GDAL】图像处理一:GDAL1.9.2在vs2010旗舰版中的配置
    【openCV】openCV2.4.8在vs2010旗舰版中的配置
    【c++】大数相加
  • 原文地址:https://www.cnblogs.com/zhanglingbing/p/10974870.html
Copyright © 2011-2022 走看看