文件上传并通过nginx做静态代理

---------=--------------java代码开始-------------------------------
package com.perye.dokit.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class UploadFile {
    /**
     *
     * @param uploadFile
     * @param uploadFilePath 文件上传路径
     * @param uploadFileMapping,文件映射路径
     * @return
     */

    public static String uploadFile(MultipartFile uploadFile, String uploadFilePath,String uploadFileMapping) {
        //1、基本校验
        if (StringUtils.isBlank(uploadFile.getOriginalFilename()) || uploadFile == null ) {
            return null;
        }
        //2、配置文件路径+文件模块名称+UUID文件名
        StringBuffer uploadPath = new StringBuffer(uploadFilePath);
        uploadPath.append(File.separator);
        String suffix = FileUtil.getExtensionName(uploadFile.getOriginalFilename());
        uploadPath.append(UuidUtil.get32UUID() + "." + suffix);
        //3、文件上传
        File file = upload(uploadFile, uploadPath.toString());
        //4、返回映射路径
        String httpPath = null == file ? null : uploadPath.replace(0, uploadFilePath.length(), uploadFileMapping).toString();
        return httpPath.replace("\\", "/");
    }
    /**
     * 将文件名解析成文件的上传路径
     *
     * @param file  上传文件
     * @param filePath 上传文件目录名称
     * @return 执行结果
     
     */
    private static File upload(MultipartFile file, String filePath) {
        try {
            // getCanonicalFile 可解析正确各种路径
            File dest = new File(filePath).getCanonicalFile();
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 文件写入
            file.transferTo(dest);
            return dest;
        } catch (Exception e) {
        }
        return null;
    }
}
---------=--------------java代码结束-------------------------------
nginx配置

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
   sendfile        on;
 
  keepalive_timeout  65;

  

    server {
        listen       8888;
        server_name  localhost;

		location ^~ /resource/ {
            alias   D:/resource/;#文件存储路径
            index  index.html index.htm;
        }
		location ^~ /herbs/ {
            alias   F:/localhostProject/pc_square_root/src/main/webapp/res/image/herbs/;
            index  index.html index.htm;
        }
        #location / {
           # root   html;
           # index  index.html index.htm;
      #  }

        #error_page  404              /404.html;

         error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值