package com.doumee.api.common; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.doumee.api.BaseController; 
 | 
import com.doumee.biz.system.SystemDictDataBiz; 
 | 
import com.doumee.core.annotation.excel.ExcelToPdfTool; 
 | 
import com.doumee.core.annotation.trace.Trace; 
 | 
import com.doumee.core.utils.Constants; 
 | 
import com.doumee.core.utils.DateUtil; 
 | 
import com.doumee.core.utils.aliyun.ALiYunUtil; 
 | 
import io.swagger.annotations.Api; 
 | 
import io.swagger.annotations.ApiImplicitParam; 
 | 
import io.swagger.annotations.ApiImplicitParams; 
 | 
import io.swagger.annotations.ApiOperation; 
 | 
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.RequestParam; 
 | 
import org.springframework.web.bind.annotation.RestController; 
 | 
import org.springframework.web.multipart.MultipartFile; 
 | 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
 | 
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.PrintWriter; 
 | 
import java.util.HashMap; 
 | 
import java.util.Iterator; 
 | 
import java.util.Map; 
 | 
import java.util.UUID; 
 | 
  
 | 
/** 
 | 
 * @author Eva.Caesar Liu 
 | 
 * @date 2023/02/14 11:14 
 | 
 */ 
 | 
@Api(tags = "公共接口") 
 | 
@Trace(exclude = true) 
 | 
@RestController 
 | 
@RequestMapping("/public") 
 | 
@Slf4j 
 | 
public class PublicController extends BaseController { 
 | 
  
 | 
  
 | 
    @Autowired 
 | 
    private SystemDictDataBiz systemDictDataBiz; 
 | 
    @ApiOperation("测试") 
 | 
    @PostMapping("/testPdf") 
 | 
    public String testPdf(@RequestParam Map<String,String> req, HttpServletResponse response) { 
 | 
        ExcelToPdfTool.excelToPdf(req.get("f1"),req.get("f2")); 
 | 
        return JSONObject.toJSONString(req); 
 | 
    } 
 | 
  
 | 
    @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", "对不起,文件格式上传有误!"); 
 | 
                return; 
 | 
            } 
 | 
        } 
 | 
        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()); 
 | 
    } 
 | 
  
 | 
    public void upload(HttpServletRequest request, HttpServletResponse response, String folder, String bucketName, 
 | 
        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<>(); 
 | 
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); 
 | 
        if (multipartResovler.isMultipart(request)) { 
 | 
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
 | 
            Iterator<String> it = multipartRequest.getFileNames(); 
 | 
            while (it.hasNext()) { 
 | 
                MultipartFile file = multipartRequest.getFile((String) it.next());// file 
 | 
                // multipartRequest.getFile((String) 
 | 
                // it.next()); 
 | 
                if (file != null) { 
 | 
                    // 1、上传到服务器临时文件夹 
 | 
                    String uploadFileName = file.getOriginalFilename(); 
 | 
                    String originname = uploadFileName; 
 | 
                    if (originname.lastIndexOf("/") >= 0) { 
 | 
                        originname = originname.substring(originname.lastIndexOf("/") + 1); 
 | 
                    } 
 | 
                    String nfix = "";// 后缀名 
 | 
                    if (StringUtils.isNotBlank(uploadFileName)) { 
 | 
                        nfix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); 
 | 
                    } 
 | 
                    if (StringUtils.equalsIgnoreCase(nfix, ".exe")) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "对不起,文件格式\".exe\"上传有误!"); 
 | 
                        return; 
 | 
                    } 
 | 
                    if (StringUtils.equalsIgnoreCase(nfix, ".dll")) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "对不起,文件格式\".dll\"上传有误!"); 
 | 
                        return; 
 | 
                    } 
 | 
                    String nowDate = DateUtil.getNowShortDate();// 当前时间(年月日) 
 | 
                    String fileName = UUID.randomUUID().toString() + nfix; 
 | 
                    String tempFileName =  nowDate + "/" + fileName; 
 | 
                    String key = folder + tempFileName;// 文件名 
 | 
                    ALiYunUtil obs = new ALiYunUtil(endpoint,access_id, access_key); 
 | 
                    int type = 2; 
 | 
                    if(StringUtils.endsWith(key,".mp4")){ 
 | 
                        type =1; 
 | 
                    }else if(StringUtils.endsWith(key,".jpg") 
 | 
                            || StringUtils.endsWith(key,".JPG") 
 | 
                            || StringUtils.endsWith(key,".png") 
 | 
                            || StringUtils.endsWith(key,".PNG") 
 | 
                            || StringUtils.endsWith(key,".jpeg") 
 | 
                            || StringUtils.endsWith(key,".JPEG")){ 
 | 
                        type =0; 
 | 
                    } 
 | 
                    if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) { 
 | 
                        // 移动成功,返回文件名 
 | 
                        // sendSuccessMessage(response, resourcePath+key); 
 | 
                        context.put("success", true); 
 | 
                        context.put("code", 200); 
 | 
                        context.put("errno",0); 
 | 
                        JSONObject fileJSON = new JSONObject(); 
 | 
                        fileJSON.put("url", resourcePath + key); 
 | 
                        fileJSON.put("imgaddr", tempFileName); 
 | 
                        fileJSON.put("imgname", fileName); 
 | 
                        fileJSON.put("type", type); 
 | 
                        fileJSON.put("originname", originname); 
 | 
                        context.put("data",fileJSON); 
 | 
                        context.put("message","请求成功"); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } else { 
 | 
                        // 移动失败 
 | 
                        context.put("code", 0); 
 | 
                        context.put("message", "上传失败"); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } 
 | 
                } 
 | 
  
 | 
            } 
 | 
        } 
 | 
        context.put("code", 0); 
 | 
        context.put("message", "上传失败"); 
 | 
        context.put("errno",0); 
 | 
        writerJson(response, context); 
 | 
        return; 
 | 
    } 
 | 
  
 | 
    public static void writerJson(HttpServletResponse response, Object object) { 
 | 
        response.setContentType("application/json"); 
 | 
        writer(response, JSONObject.toJSONString(object)); 
 | 
    } 
 | 
  
 | 
    private static void writer(HttpServletResponse response, String str) { 
 | 
        try { 
 | 
            StringBuffer result = new StringBuffer(); 
 | 
            //设置页面不缓存 
 | 
            response.setHeader("Pragma", "No-cache"); 
 | 
            response.setHeader("Cache-Control", "no-cache"); 
 | 
            response.setCharacterEncoding("UTF-8"); 
 | 
            PrintWriter out = null; 
 | 
            out = response.getWriter(); 
 | 
            out.print(str); 
 | 
            out.flush(); 
 | 
            out.close(); 
 | 
        } catch (IOException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    @ApiOperation(value = "上传", notes = "上传", httpMethod = "POST", position = 6) 
 | 
    @ApiImplicitParams({ 
 | 
            @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), 
 | 
    }) 
 | 
    @PostMapping(value = "/uploadLocal", headers = "content-type=multipart/form-data") 
 | 
    public void uploadLocal(String folder, HttpServletRequest request, HttpServletResponse response) throws Exception { 
 | 
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
 | 
  
 | 
        // CommonsMultipartFile files = (CommonsMultipartFile) multipartRequest.getFile("filedata"); 
 | 
        uploadFileLocal(multipartRequest,  folder+ "/", response, 
 | 
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.GOODS_IMG_DIR).getCode() , 
 | 
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode()); 
 | 
  
 | 
    } 
 | 
  
 | 
    public void uploadFileLocal(HttpServletRequest request, String folder, HttpServletResponse response, String rootPath,String dir) throws Exception { 
 | 
        response.setCharacterEncoding("UTF-8"); 
 | 
        response.setContentType("text/html;charset=UTF-8"); 
 | 
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); 
 | 
        Map<String, Object> context = new HashMap<>(); 
 | 
        if (multipartResovler.isMultipart(request)) { 
 | 
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
 | 
            Iterator<String> it = multipartRequest.getFileNames(); 
 | 
            while (it.hasNext()) { 
 | 
                MultipartFile file = multipartRequest.getFile((String) it.next());// file 
 | 
                // = 
 | 
                // (CommonsMultipartFile) 
 | 
                // multipartRequest.getFile((String) 
 | 
                // it.next()); 
 | 
                if (file != null) { 
 | 
                    if (file.getSize() > 200 * 1024 * 1024L) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "上传文件过大"); 
 | 
                        return; 
 | 
                    } 
 | 
                    System.out.println(file.getOriginalFilename()); 
 | 
                    if (file.getOriginalFilename() == null) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "文件名不可为空"); 
 | 
                        return; 
 | 
                    } 
 | 
                    /* 
 | 
                     * if(file.getOriginalFilename().contains(",")||file.getOriginalFilename(). 
 | 
                     * contains(" ")){ sendFailureMessage(response,"文件名称有误,不可含有逗号等特殊字符"); } 
 | 
                     */ 
 | 
  
 | 
                    String nowDate = DateUtil.getNowShortDate(); 
 | 
                    folder += nowDate + "/"; 
 | 
                    String strDirPath = rootPath + folder; 
 | 
                    File dirPath = new File(strDirPath); 
 | 
                    if (!dirPath.exists()) { 
 | 
                        dirPath.mkdirs(); 
 | 
                    } 
 | 
                    String uploadFileName = file.getOriginalFilename(); 
 | 
                    String x = UUID.randomUUID().toString().replace("-", "") 
 | 
                            + uploadFileName.substring(uploadFileName.lastIndexOf(".")); 
 | 
                    String fileName = folder + x; 
 | 
                    String fileNames = nowDate + "/" + x; 
 | 
                    uploadFileName = uploadFileName.replace(" ", ""); 
 | 
                    uploadFileName = uploadFileName.replace(",", ","); 
 | 
                    uploadFileName = uploadFileName.replaceAll(",", "-"); 
 | 
                    System.err.println("R:" + fileName); 
 | 
                    String fileAndPath = dir + fileName; 
 | 
                    System.err.println("A:" + fileAndPath); 
 | 
                    // 判断如果临时目录中存在相同名称的文件先删除,在上传 
 | 
                    File tempFile = new File(rootPath + fileName); 
 | 
                    if (tempFile.isFile() && tempFile.exists()) { 
 | 
                        tempFile.getAbsoluteFile().delete(); 
 | 
                    } 
 | 
                    // 上传到服务器临时文件夹 
 | 
                    file.transferTo(tempFile); 
 | 
                    // 转移到FTP服务器 
 | 
                    String nfix = ""; 
 | 
                    if (null != uploadFileName) { 
 | 
                        nfix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); 
 | 
                    } 
 | 
  
 | 
                    if (StringUtils.equalsIgnoreCase(nfix, ".exe")) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "对不起,文件格式\".exe\"上传有误!"); 
 | 
                        return; 
 | 
                    } 
 | 
                    if (StringUtils.equalsIgnoreCase(nfix, ".dll")) { 
 | 
                        context.put("code", 4000); 
 | 
                        context.put("message", "对不起,文件格式\".dll\"上传有误!"); 
 | 
                        return; 
 | 
                    } 
 | 
  
 | 
                    String remoteName = fileName; 
 | 
                    String remoteFileName = fileAndPath; 
 | 
  
 | 
             /*       Map map = new HashMap(); 
 | 
                    map.put("url", remoteFileName); 
 | 
                    map.put("alt", uploadFileName); 
 | 
                    map.put("href", remoteFileName); 
 | 
                    List maps = Arrays.asList(remoteFileName);*/ 
 | 
  
 | 
                    if (true) { 
 | 
                        // 关闭FTP流 
 | 
                        // 移动成功,返回文件名 
 | 
                        /*context.put("error", 0); 
 | 
                        context.put("url", remoteFileName); 
 | 
                        context.put("fullurl", remoteFileName); 
 | 
                        context.put("fname", uploadFileName); 
 | 
                        context.put("data", maps); 
 | 
                        context.put("halfurl", fileNames);*/ 
 | 
  
 | 
                        context.put("success", true); 
 | 
                        context.put("code", 200); 
 | 
                        context.put("errno",0); 
 | 
                        JSONObject fileJSON = new JSONObject(); 
 | 
                        fileJSON.put("url", remoteFileName); 
 | 
                        fileJSON.put("imgaddr", fileNames); 
 | 
                        fileJSON.put("imgname", uploadFileName); 
 | 
                        fileJSON.put("originname", uploadFileName); 
 | 
                        context.put("data",fileJSON); 
 | 
                        context.put("message","请求成功"); 
 | 
  
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } else { 
 | 
                        // 移动失败 
 | 
                        context.put("code", 0); 
 | 
                        context.put("message", "上传失败"); 
 | 
                        writerJson(response, context); 
 | 
                    } 
 | 
                } 
 | 
                context.put("code", 0); 
 | 
                context.put("message", "上传失败"); 
 | 
                writerJson(response, context); 
 | 
                return; 
 | 
            } 
 | 
        } 
 | 
        context.put("code", 0); 
 | 
        context.put("message", "上传失败"); 
 | 
        writerJson(response, context); 
 | 
        return; 
 | 
    } 
 | 
} 
 |