package doumeemes.api; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import doumeemes.biz.system.SystemDictDataBiz; 
 | 
import doumeemes.core.model.LoginUserInfo; 
 | 
import doumeemes.core.utils.DateUtil; 
 | 
import doumeemes.core.utils.aliyun.ALiYunUtil; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.apache.commons.lang3.StringUtils; 
 | 
import org.apache.shiro.SecurityUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
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.IOException; 
 | 
import java.io.PrintWriter; 
 | 
import java.util.HashMap; 
 | 
import java.util.Iterator; 
 | 
import java.util.Map; 
 | 
import java.util.UUID; 
 | 
  
 | 
/** 
 | 
 * Controller基类 
 | 
 * @author Eva.Caesar Liu 
 | 
 * @date 2022/04/18 18:12 
 | 
 */ 
 | 
@Slf4j 
 | 
public class BaseController { 
 | 
  
 | 
    @Autowired 
 | 
    public SystemDictDataBiz systemDictDataBiz; 
 | 
  
 | 
    /** 
 | 
     * 获取当前登录用户 
 | 
     * @author Eva.Caesar Liu 
 | 
     * @date 2022/04/18 18:12 
 | 
     */ 
 | 
    protected LoginUserInfo getLoginUser () { 
 | 
        return (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); 
 | 
    } 
 | 
  
 | 
  
 | 
    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", 0); 
 | 
                        context.put("message", "对不起,文件格式\".exe\"上传有误!"); 
 | 
                        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); 
 | 
                    if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) { 
 | 
                        // 移动成功,返回文件名 
 | 
                        // sendSuccessMessage(response, resourcePath+key); 
 | 
                        context.put("error", 0); 
 | 
                        context.put("code", 200); 
 | 
                        context.put("url", resourcePath + key); 
 | 
                        context.put("imgaddr", tempFileName); 
 | 
                        context.put("imgname", fileName); 
 | 
                        context.put("originname", originname); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } else { 
 | 
                        // 移动失败 
 | 
                        context.put("code", 0); 
 | 
                        context.put("message", "上传失败"); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } 
 | 
                } 
 | 
  
 | 
            } 
 | 
        } 
 | 
        context.put("code", 0); 
 | 
        context.put("message", "上传失败"); 
 | 
        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(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    public void upload2(MultipartFile file, 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(); 
 | 
  
 | 
  
 | 
                // 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", 0); 
 | 
                        context.put("message", "对不起,文件格式\".exe\"上传有误!"); 
 | 
                        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); 
 | 
                    if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) { 
 | 
                        // 移动成功,返回文件名 
 | 
                        // sendSuccessMessage(response, resourcePath+key); 
 | 
  
 | 
  
 | 
  
 | 
                        Map<String, Object> data = new HashMap<>(); 
 | 
                        data.put("url", resourcePath + key); 
 | 
                        data.put("imgaddr", tempFileName); 
 | 
                        data.put("imgname", fileName); 
 | 
                        data.put("originname", originname); 
 | 
  
 | 
                        context.put("error", 0); 
 | 
                        context.put("code", 200); 
 | 
                        context.put("data", data); 
 | 
                        context.put("success", true); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } else { 
 | 
                        // 移动失败 
 | 
                        context.put("code", 0); 
 | 
                        context.put("message", "上传失败"); 
 | 
                        context.put("success", false); 
 | 
                        writerJson(response, context); 
 | 
                        return; 
 | 
                    } 
 | 
                } 
 | 
  
 | 
  
 | 
        context.put("code", 0); 
 | 
        context.put("message", "上传失败"); 
 | 
        writerJson(response, context); 
 | 
        return; 
 | 
    } 
 | 
} 
 |