All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.util.upload.UploadUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.util.upload;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luues.util.SystemUtil;
import com.luues.util.TypeConvert;
import com.luues.util.logs.LogUtil;
import com.luues.util.shorts.ShortUrlGenerator;
import com.luues.util.uuid.JUUID;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

public class UploadUtil {

    protected final String uoload_url = "/wdy_upload/";
    protected final String ngxin_window_path = "C://Windows/rsa_key_file/images";
    protected final String ngxin_linux_path = "/home/rsa_key_file/images";
    protected FileInfo fileInfo;
    public UploadUtil(){
        this.fileInfo = new FileInfo();
    }

    protected FileInfo getFileInfo() {
        return this.fileInfo;
    }

    protected void setFileInfo(MultipartFile multipartFile){
        String originalFilename = multipartFile.getOriginalFilename();
        this.fileInfo.setFileType(originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase());
        this.fileInfo.setFileSize(multipartFile.getSize());
        this.fileInfo.setFileGenerationName(multipartFile.getOriginalFilename());
    }

    public FileInfo uploadFile(HttpServletRequest request, MultipartFile file, String paths, boolean nginx, Boolean shor, boolean autoFileName) {
        setFileInfo(file);
        Calendar calendar = Calendar.getInstance();
        paths = getNginx(request, nginx, paths);
        if (paths.lastIndexOf("/") != paths.length() - 1) {
            paths += "/";
        }
        String path = paths + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + "/";
        File files = new File(path);
        if (!files.exists()) {
            files.mkdirs();
        }
        OutputStream os = null;
        String fileName = "";
        String originalFilename = file.getOriginalFilename();
        try {
            if(autoFileName){
                fileName = JUUID.generateUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            }else{
                fileName = originalFilename;
            }
            os = new FileOutputStream(new File(path, fileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            os.write(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.fileInfo.setDb_lang_fileUrl(path + fileName);
        this.fileInfo.setFileName(fileName);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", this.fileInfo.getFileName());
        jsonObject.put("size", this.fileInfo.getFileSize());
        jsonObject.put("type", this.fileInfo.getFileType());
        jsonObject.put("path", this.fileInfo.getDb_lang_fileUrl());
        if (shor) {
            String shortUrl = ShortUrlGenerator.createShortUrl(JSON.toJSONString(jsonObject));
            this.fileInfo.setDb_short_fileUrl(shortUrl);
        }
        this.fileInfo.setFid(JUUID.generateUUID());
        return this.fileInfo;
    }

    public FileInfo baseUpload(HttpServletRequest request, String base64, String paths, String fileName, String fileType, String delOldImg, boolean nginx, boolean shor) {
        paths = getNginx(request, nginx, paths);
        if (paths.lastIndexOf("/") != paths.length() - 1) {
            paths += "/";
        }
        Calendar calendar = Calendar.getInstance();
        String path = paths + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + "/";
        File files = new File(path);
        if (!files.exists()) {
            files.mkdirs();
        }
        if (TypeConvert.isNull(fileType)) {
            fileType = "image/jpg";
        }
        if (!TypeConvert.isNull(fileType)) {
            if (!fileType.contains("image/")) {
                this.fileInfo.setMessage("文件类型有误");
                return this.fileInfo;
            }
            fileType = "." + fileType.split("image/")[1];
        }
        String name = JUUID.generateUUID() + fileType;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 判断是否正确的base64格式
            if (base64.indexOf(";base64,") > -1) {
                base64 = base64.split(";base64,")[1];
            }
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(base64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            File file = new File(path, name);
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", file.getName());
            jsonObject.put("size", file.length());
            jsonObject.put("type", fileType);
            jsonObject.put("path", file.getPath());
            this.fileInfo.setDb_lang_fileUrl(path + name);
            this.fileInfo.setFileName(name);
            if(shor){
                String shortUrl = ShortUrlGenerator.createShortUrl(JSON.toJSONString(jsonObject));
                this.fileInfo.setDb_short_fileUrl(shortUrl);
            }
            if (!TypeConvert.isNull(delOldImg)) {
                del(delOldImg);
            }
            this.fileInfo.setFid(JUUID.generateUUID());
        } catch (Exception e) {
            LogUtil.error("base64上传错误--->{"+ e.getMessage() +"}");
            this.fileInfo.setMessage("base64上传错误--->{\"+ e.getMessage() +\"}");
        }
        return this.fileInfo;
    }


    protected String getNginx(HttpServletRequest request, boolean nginx, String paths){
        if (!nginx) {
            if (TypeConvert.isNull(paths)) {
                return request.getRealPath(this.uoload_url);
            }
        } else {
            if (SystemUtil.isWindows()) {
                return ngxin_window_path;
            } else {
                return ngxin_linux_path;
            }
        }
        return paths;
    }

    protected void del(String img_path) {
        File file = new File(img_path);
        if (!file.exists()) {

        } else {
            file.delete();
        }
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy