| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package com.goafanti.common.controller;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.multipart.MultipartRequest;
- import com.goafanti.common.bo.Result;
- import com.goafanti.common.utils.FileUtils;
- import com.goafanti.common.utils.LoggerUtils;
- import com.goafanti.core.shiro.token.TokenManager;
- @RestController
- @RequestMapping(value = "/api")
- public class BaseApiController extends BaseController {
- @Value(value = "${upload.path}")
- private String uploadPath = null;
- @Value(value = "${upload.private.path}")
- private String uploadPrivatePath = null;
- /**
- *
- * @param request
- * @return
- */
- protected List<MultipartFile> getFiles(HttpServletRequest request) {
- if (ServletFileUpload.isMultipartContent(request)) {
- Iterator<String> itr = ((MultipartRequest) request).getFileNames();
- List<MultipartFile> files = new ArrayList<MultipartFile>();
- if (itr.hasNext()) {
- files.add(((MultipartRequest) request).getFile(itr.next()));
- }
- return files;
- }
- return new ArrayList<MultipartFile>();
- }
- /**
- *
- * @param res
- * @param path
- * @param isPrivate
- * @param req
- * @param sign
- * @return
- */
- protected String handleFile(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign) {
- List<MultipartFile> files = getFiles(req);
- String fileName = "";
- if (!files.isEmpty()) {
- try {
- MultipartFile mf = files.get(0);
- fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path, TokenManager.getUserId());
- mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
- LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
- } catch (Exception e) {
- LoggerUtils.error(getClass(), "文件上传失败", e);
- res.getError().add(buildError("", "文件上传失败!"));
- }
- } else {
- res.getError().add(buildError("", "文件上传失败!"));
- }
- return fileName;
- }
- /**
- *
- * @param res
- * @param path
- * @param isPrivate
- * @param req
- * @param sign
- * @return
- */
- protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
- List<MultipartFile> files = getFiles(req);
- String realFilePath = "";
- if (!files.isEmpty()) {
- try {
- MultipartFile mf = files.get(0);
- realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
- File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
- mf.transferTo(f);
- LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
- } catch (Exception e) {
- LoggerUtils.error(getClass(), "文件上传失败", e);
- res.getError().add(buildError("", "文件上传失败!"));
- }
- } else {
- res.getError().add(buildError("", "文件上传失败!"));
- }
- return realFilePath;
- }
- /**
- *
- * @param res
- * @param fileName
- * @param filePath
- */
- protected void downloadFile(HttpServletResponse res, String fileName, String filePath) {
- FileUtils.downloadFile(res, fileName, uploadPrivatePath + filePath);
- }
- protected void downloadUnPrivateFile(HttpServletResponse res, String fileName, String filePath) {
- FileUtils.downloadFile(res, fileName, uploadPath + filePath);
- }
- private File buildFile(String filePath) {
- File toFile = new File(filePath);
- toFile.mkdirs();
- return toFile;
- }
- /**
- *
- * @param fileName
- * @return
- */
- protected File toFile(String fileName) {
- return buildFile(uploadPath + fileName);
- }
- /**
- *
- * @param fileName
- * @return
- */
- protected File toPrivateFile(String fileName) {
- return buildFile(uploadPrivatePath + fileName);
- }
- /**
- *
- * @param fileName
- * @return
- */
- protected File toAdminPrivateFile(String fileName) {
- return buildFile(uploadPrivatePath + FileUtils.buildFilePath("/admin/", fileName));
- }
- protected Integer getPageNo(String pageNo) {
- if (StringUtils.isNumeric(pageNo)) {
- Integer pn = Integer.parseInt(pageNo);
- return pn < 1 ? 1 : pn;
- }
- return 1;
- }
- protected Integer getPageSize(String pageSize) {
- if (StringUtils.isNumeric(pageSize)) {
- Integer ps = Integer.parseInt(pageSize);
- return ps > 50 ? 50 : (ps < 10 ? 10 : ps);
- }
- return 10;
- }
- protected String handleFiles(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign,
- String uid) {
- List<MultipartFile> files = getFiles(req);
- MultipartFile mf = files.get(0);
- String fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path,
- StringUtils.isBlank(uid) ? TokenManager.getUserId() : uid);
- if (!files.isEmpty()) {
- try {
- if (isPrivate) {
- mf.transferTo(toPrivateFile(fileName));
- } else {
- mf.transferTo(toFile(fileName));
- }
- LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
- } catch (IllegalStateException | IOException e) {
- LoggerUtils.error(getClass(), "文件上传失败", e);
- res.getError().add(buildError("", "文件上传失败!"));
- return "";
- }
- } else {
- res.getError().add(buildError("", "文件上传失败!"));
- return "";
- }
- return fileName;
- }
- protected Integer handlePageNo(String pageNo) {
- try {
- Integer n = Integer.valueOf(pageNo);
- return n < 1 ? 1 : n;
- } catch (Exception e) {
- return 1;
- }
- }
- protected Integer handlePageSize(String pageSize) {
- try {
- Integer s = Integer.valueOf(pageSize);
- return s < 5 ? 5 : s > 100 ? 100 : s;
- } catch (Exception e) {
- return 10;
- }
- }
- protected Result res() {
- return new Result();
- }
-
-
- @SuppressWarnings("resource")
- protected String handleBaseFiles(Result res, String path, boolean isPrivate, byte[] bs, String sign,
- String uid,String fn) {
- String fileName = FileUtils.mosaicFileName("", isPrivate, path, StringUtils.isBlank(uid) ? TokenManager.getUserId() : uid);
- OutputStream fos = null;
- if (bs != null && bs.length>0) {
- try {
- if (isPrivate) {
- File filePath = new File(uploadPrivatePath + fileName);
- if(!filePath.exists()){
- filePath.mkdirs();
- }
- fileName += "/"+ fn;
- System.out.println("------------"+ uploadPrivatePath + fileName);
- fos = new FileOutputStream(new File(uploadPrivatePath + fileName));
- } else {
- File filePath = new File(uploadPath + fileName);
- if(!filePath.exists()){
- filePath.mkdirs();
- }
- fileName += "/" + fn;
- System.out.println("------------"+ uploadPath + fileName);
- fos = new FileOutputStream(new File(uploadPath + fileName));
- }
- fos.write(bs);
- LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
- } catch (IllegalStateException | IOException e) {
- LoggerUtils.error(getClass(), "文件上传失败", e);
- res.getError().add(buildError("", "文件上传失败!"));
- return "";
- }
- } else {
- res.getError().add(buildError("", "文件上传失败!"));
- return "";
- }
- return fileName;
- }
- }
|