package doumeemes.api.common; import com.alibaba.fastjson.JSONObject; import doumeemes.api.BaseController; import doumeemes.biz.system.SystemDictDataBiz; import doumeemes.core.model.LoginUserInfo; import doumeemes.core.utils.Constants; import doumeemes.core.utils.DateUtil; import doumeemes.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.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; 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.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 @Api(tags = "通用接口类") @RestController public class CommonController extends BaseController { @Autowired private SystemDictDataBiz systemDictDataBiz; /** * 获取当前登录用户 * @author Eva.Caesar Liu * @date 2022/04/18 18:12 */ @Override protected LoginUserInfo getLoginUser () { return (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); } /** * 手机端上传 * * @param folder * @param request * @param response * @throws Exception */ @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()); } // // 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 context = new HashMap<>(); // CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); // if (multipartResovler.isMultipart(request)) { // MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // Iterator 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", 1); // 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(); // } // } }