---------=--------------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;
}
}
}
文件上传并通过nginx做静态代理
最新推荐文章于 2024-07-20 23:37:21 发布