|  |  | 
 |  |  | import com.doumee.core.annotation.trace.Trace; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.core.utils.DateUtil; | 
 |  |  | import com.doumee.core.utils.FtpUtil; | 
 |  |  | import com.doumee.core.utils.aliyun.ALiYunUtil; | 
 |  |  | import io.swagger.annotations.Api; | 
 |  |  | import io.swagger.annotations.ApiImplicitParam; | 
 |  |  | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RestController; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  | import org.springframework.web.multipart.MultipartHttpServletRequest; | 
 |  |  | import org.springframework.web.multipart.commons.CommonsMultipartFile; | 
 |  |  | import org.springframework.web.multipart.commons.CommonsMultipartResolver; | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletRequest; | 
 |  |  | import javax.servlet.http.HttpServletResponse; | 
 |  |  | import java.io.File; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.io.InputStream; | 
 |  |  | import java.io.PrintWriter; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.Iterator; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.UUID; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author Eva.Caesar Liu | 
 |  |  | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @ApiOperation(value = "上传", notes = "上传", httpMethod = "POST", position = 6) | 
 |  |  |     @ApiImplicitParams({ | 
 |  |  |         @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
 |  |  |     }) | 
 |  |  |     @PostMapping(value = "/upload", headers = "content-type=multipart/form-data") | 
 |  |  |     public void uploadMobile(String folder, HttpServletRequest request, HttpServletResponse response) throws Exception { | 
 |  |  |         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
 |  |  |         upload(multipartRequest, response, folder + "/", | 
 |  |  |             systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode(), | 
 |  |  |             systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode(), | 
 |  |  |             systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode(), | 
 |  |  |             systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode(), | 
 |  |  |             systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode()); | 
 |  |  |     } | 
 |  |  | //    @ApiOperation(value = "上传", notes = "上传", httpMethod = "POST", position = 6) | 
 |  |  | //    @ApiImplicitParams({ | 
 |  |  | //            @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
 |  |  | //    }) | 
 |  |  | //    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data") | 
 |  |  | //    public void uploadMobile(String folder, HttpServletRequest request, HttpServletResponse response) throws Exception { | 
 |  |  | //        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
 |  |  | //        upload(multipartRequest, response, folder + "/", | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode()); | 
 |  |  | //    } | 
 |  |  | // | 
 |  |  | // | 
 |  |  | //    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST", position = 6) | 
 |  |  | //    @ApiImplicitParams({ | 
 |  |  | //            @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
 |  |  | //    }) | 
 |  |  | //    @PostMapping(value = "/uploadPicture", headers = "content-type=multipart/form-data") | 
 |  |  | //    public void uploadPicture(String folder, HttpServletRequest request, HttpServletResponse response) throws Exception { | 
 |  |  | //        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
 |  |  | //        Iterator<String> fileNames = multipartRequest.getFileNames(); | 
 |  |  | //        Map<String, Object> context = new HashMap<>(); | 
 |  |  | //        while (fileNames.hasNext()){ | 
 |  |  | //            MultipartFile file = multipartRequest.getFile(fileNames.next()); | 
 |  |  | //            String fileName = file.getOriginalFilename(); | 
 |  |  | //            String suffix = fileName.substring(fileName.lastIndexOf(".")); | 
 |  |  | //            if ( !StringUtils.equalsIgnoreCase(suffix, ".jpg") || !StringUtils.equalsIgnoreCase(suffix, ".png")) { | 
 |  |  | //                context.put("code", 0); | 
 |  |  | //                context.put("message", "对不起,文件格式上传有误!"); | 
 |  |  | //            } | 
 |  |  | //        } | 
 |  |  | //        upload(multipartRequest, response, folder + "/", | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode(), | 
 |  |  | //                systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode()); | 
 |  |  | //    } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST", position = 6) | 
 |  |  |     @ApiImplicitParams({ | 
 |  |  |             @ApiImplicitParam(name = "folder", value = "banner/ (轮播)goods/ (商品)member/ (用户)shop/ (店铺)system/ (系统配置等)activity/ (发现)commet/ (评论)other/ (其他)aftersale/(售后)", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
 |  |  |     }) | 
 |  |  |     @PostMapping(value = "/uploadPicture", headers = "content-type=multipart/form-data") | 
 |  |  |     public void uploadPicture(String folder, HttpServletRequest request, HttpServletResponse response) throws Exception { | 
 |  |  |         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
 |  |  |         Iterator<String> fileNames = multipartRequest.getFileNames(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public static FtpUtil ftp  = null; | 
 |  |  |  | 
 |  |  |     @ApiOperation(value = "上传文件到FTP") | 
 |  |  |     @RequestMapping(method= RequestMethod.POST,value="upload") | 
 |  |  |     @ResponseBody | 
 |  |  |     public void upload(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { | 
 |  |  | //        folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); | 
 |  |  |         String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); | 
 |  |  |         InputStream is = null; | 
 |  |  |         response.setCharacterEncoding("UTF-8"); | 
 |  |  |         response.setContentType("text/html;charset=UTF-8"); | 
 |  |  |         Map<String, Object> context = new HashMap<>(); | 
 |  |  |         while (fileNames.hasNext()){ | 
 |  |  |             MultipartFile file = multipartRequest.getFile(fileNames.next()); | 
 |  |  |             String fileName = file.getOriginalFilename(); | 
 |  |  |             String suffix = fileName.substring(fileName.lastIndexOf(".")); | 
 |  |  |             if ( !StringUtils.equalsIgnoreCase(suffix, ".jpg") || !StringUtils.equalsIgnoreCase(suffix, ".png")) { | 
 |  |  |                 context.put("code", 0); | 
 |  |  |                 context.put("message", "对不起,文件格式上传有误!"); | 
 |  |  |         try { | 
 |  |  |             if(ftp == null){ | 
 |  |  |                 ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), | 
 |  |  |                         Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), | 
 |  |  |                         systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), | 
 |  |  |                         systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); | 
 |  |  |             }else{ | 
 |  |  |                 ftp.connect(); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); | 
 |  |  |             if (multipartResovler.isMultipart(request)) { | 
 |  |  |                 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
 |  |  |                 Iterator<String> it = multipartRequest.getFileNames(); | 
 |  |  |                 while (it.hasNext()) { | 
 |  |  |                     MultipartFile file = multipartRequest.getFile(it.next()); | 
 |  |  |                     is = file.getInputStream(); | 
 |  |  |                     String date = DateUtil.getNowShortDate(); | 
 |  |  |                     String fName =  date+"/"+ UUID.randomUUID()+".jpg"; | 
 |  |  |                     String fileName = folder+fName; | 
 |  |  |                     boolean r = ftp.uploadInputstream(is,fileName); | 
 |  |  |                     if(r){ | 
 |  |  |                         context.put("success", true); | 
 |  |  |                         context.put("code", 200); | 
 |  |  |                         context.put("errno",0); | 
 |  |  |                         JSONObject fileJSON = new JSONObject(); | 
 |  |  |                         fileJSON.put("halfPath", fName); | 
 |  |  |                         fileJSON.put("prefixPath", prefixPath); | 
 |  |  |                         fileJSON.put("folder", folder); | 
 |  |  |                         context.put("data",fileJSON); | 
 |  |  |                         context.put("message","请求成功"); | 
 |  |  |                         writerJson(response, context); | 
 |  |  |                         return; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("【上传FTP失败】======================"+e.getMessage()); | 
 |  |  |         } | 
 |  |  |         upload(multipartRequest, response, folder + "/", | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode(), | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode(), | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode(), | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode(), | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode()); | 
 |  |  |         context.put("code", 0); | 
 |  |  |         context.put("message", "上传失败"); | 
 |  |  |         context.put("errno",0); | 
 |  |  |         writerJson(response, context); | 
 |  |  |         return; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public void upload(HttpServletRequest request, HttpServletResponse response, String folder, String bucketName, | 
 |  |  |         String access_id, String access_key, String resourcePath, String endpoint) throws Exception { | 
 |  |  |                        String access_id, String access_key, String resourcePath, String endpoint) throws Exception { | 
 |  |  |         response.setCharacterEncoding("UTF-8"); | 
 |  |  |         response.setContentType("text/html;charset=UTF-8"); | 
 |  |  |         Map<String, Object> context = new HashMap<>(); | 
 |  |  | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public void uploadFileLocal(HttpServletRequest request, String folder, HttpServletResponse response, String rootPath,String dir) throws Exception { | 
 |  |  | 
 |  |  |         writerJson(response, context); | 
 |  |  |         return; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |