jiangping
2025-07-15 ea99df3cd96a57e6478c20c85eaaf44ce0ad1481
server/services/src/main/java/com/doumee/api/common/PublicCloudController.java
@@ -67,77 +67,6 @@
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode());
    }
    @ApiOperation(value = "上传OBS", notes = "上传OBS", httpMethod = "POST", position = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "file", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
    })
    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    @ResponseBody
    public void upload(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception {
        Date d1 = new Date();
        log.error("总得上传文件成功=============开始========="+DateUtil.getPlusTime2(d1));
        SystemDictData folderData =  systemDictDataBiz.queryByCode(Constants.FTP,folder);
        if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"目标文件夹错误");
        }
        folder =folderData.getCode().replace("/", "");
        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<>();
        try {
            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());
                    System.out.println(file.getSize());
                    String originname = file.getOriginalFilename();
                    is = file.getInputStream();
                    String endType = ".jpg";
                    if(originname.indexOf(".")>0){
                        endType=originname.substring(originname.lastIndexOf("."),originname.length());
                    }
                    String date = DateUtil.getNowShortDate();
                    String fName =  date+"/"+ UUID.randomUUID()+endType;
                    String fileName = folder+"/"+fName;
                    boolean r =false;
                    ObsUtil obsUtil = new ObsUtil( systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ACCESSID).getCode(),
                            systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ACCESSKEY).getCode(),
                            systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ENDPOINT).getCode());
                    r = obsUtil.uploadFile( systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_BUCKET).getCode(),is,fileName);//上传
                    if(r){
                        context.put("success", true);
                        context.put("code", 200);
                        context.put("errno",0);
                        JSONObject fileJSON = new JSONObject();
//                        fileJSON.put("prefixPath", prefixPath);
//                        fileJSON.put("folder", folder);
                        fileJSON.put("url", prefixPath+fileName);
                        fileJSON.put("imgaddr", fName);
                        fileJSON.put("imgname", fileName);
                        fileJSON.put("originname", originname);
                        context.put("data",fileJSON);
                        context.put("message","请求成功");
                        writerJson(response, context);
                        Date d2= new Date();
                        log.error("总的上传文件成功=============结束========="+DateUtil.getPlusTime2(d2) +"耗时秒:"+( (d2.getTime()-d1.getTime()) /1000));
                        return;
                    }
                }
            }
        } catch (Exception e) {
            log.error("【上传FTP失败】======================"+e.getMessage());
        }
        context.put("code", 0);
        context.put("message", "上传失败");
        context.put("errno",0);
        writerJson(response, context);
        return;
    }
    public void uploadFileLocal(HttpServletRequest request,  HttpServletResponse response, String rootPath,String dir) throws Exception {