k94314517
2025-07-15 67c98cdd79a1e473fe95ddc1c695ed162c176510
server/services/src/main/java/com/doumee/api/common/PublicCloudController.java
@@ -54,18 +54,74 @@
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @ApiOperation(value = "上传", notes = "上传", httpMethod = "POST", position = 6)
    @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 = "/uploadLocal", headers = "content-type=multipart/form-data")
    public void uploadLocal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    @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));
        if(Objects.isNull(folder)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"目标文件夹错误");
        }
        String prefixPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.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;
        uploadFileLocal(multipartRequest, response,
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.IMG_DIR).getCode() ,
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode());
                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;
    }