赞
踩
本来使用springboot做的文件上传本地测试没问题,但是后端部署到公司的服务器以后,前后端联调就会上传文件后,根据返回路径访问不到图片(linux并不会让你直接根据域名/路径/文件名 的方式获取资源)。解决办法:使用nginx反向代理。
server { listen 80; server_name 域名; location / { root html; index index.html index.htm; } #以下为我添加的配置 location ~ .(jpg|png|jpeg|gif|bmp)$ { #可识别的文件后缀 root /usr/local/webserver/nginx/image/; #图片的映射路径 autoindex on; #开启自动索引 # expires 1h; #过期时间 } #至此结束 }
简单的说就是nginx监听了80端口,只要是.(jpg|png|jpeg|gif|bmp)结尾的url都会去/usr/local/webserver/nginx/image/下面找对应的文件。
#文件上传配置
spring.servlet.multipart.enabled=true
#单个文件最大大小
spring.servlet.multipart.max-file-size=30MB
#一次请求文件最大大小
spring.servlet.multipart.max-request-size=100MB
#文件保存路径
#linux下文件的上传路径
file.upload.path=/usr/local/webserver/nginx/image/
#文件访问路径
file.coming.path=http://ip:nginx监听的端口/
import cn.anson.AdminAnsonApplication; import cn.anson.model.Result; import cn.anson.model.ResultCodeEnum; import cn.anson.model.vo.UploadVO; import cn.anson.utils.MyException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; 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.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; /** * @author yhd * @createtime 2020/10/11 16:40 */ @CrossOrigin @Slf4j @RestController @RequestMapping("/api/img") public class ImgUpLoadController { @Value("${file.upload.path}") private String fileSavePath; @Value("${file.coming.path}") private String fileComingPath; @PostMapping("upload") public Result<UploadVO> updloadImg(MultipartFile upload,HttpServletRequest request){ try { //获取文件上传路径 String path = fileSavePath; log.info("文件上传路径为:"+path); File file = new File(path); //判断不存在该目录就创建 if (!file.exists()){ file.mkdirs(); } //获取文件名 String filename = upload.getOriginalFilename(); log.info(filename); //起别名 String s = UUID.randomUUID().toString().replace("-", "").toUpperCase(); filename=s+filename; //开始上传 upload.transferTo(new File(file,filename)); UploadVO uploadVO = new UploadVO(); uploadVO.setFileName(fileComingPath+filename); log.info("最终文件的访问路径:"+uploadVO.getFileName()); return Result.ok(uploadVO); } catch (IOException e) { log.error("图片上传失败"+upload.getOriginalFilename()); throw new MyException(ResultCodeEnum.SERVICE_ERROR); } } }
{
"code": 200,
"message": "成功",
"data": {
"fileName": "http://ip:nginx监听的端口/57A05F7EC8524363962403F1C539FD2Atimg.jpg"
},
"ok": true
}
点击访问这个路径
成功访问到。
<!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.3</version> </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。