上传图片后无法用浏览器访问

上传图片后无法用浏览器访问

问题描述:

调用上传图片接口后本地目录生成图片,但是浏览器访问URL提示404


相关截图:

http://img1.sycdn.imooc.com//climg/606eb583098d0c9317990953.jpg

http://img1.sycdn.imooc.com//climg/606eb5ab09ffda8813370603.jpg

http://img1.sycdn.imooc.com//climg/606eb5be0952a86017191088.jpg


相关代码:

package com.fanyu.mall.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Constant {
public static final String SALT = "qa12dsGJHJ.]@12dsj";
public static final String FANYU_MALL_USER = "fanyu_mall_user";

public static String FILE_UPLOAD_DIR;

@Value("${file.upload.dir}")
public void setFileUploadDir(String fileUploadDir) {
FILE_UPLOAD_DIR = fileUploadDir;
}
}

相关代码:

package com.fanyu.mall.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class FanyuMallMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/admin/**").addResourceLocations("classpath:/static/admin/");
registry.addResourceHandler("/images/**")
.addResourceLocations("file:D:/D1/code/java/somedata/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}

}

相关代码:

server.port=8080
spring.datasource.name=fanyu_mall_datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/fanyu_mall?serverTimezone=Asia/Shanghai
&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456


mybatis.mapper-locations=classpath:mappers/*.xml

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456

file.upload.dir=D:/D1/code/java/somedata/

相关代码:

​package com.fanyu.mall.controller;

import com.fanyu.mall.common.ApiRestResponse;
import com.fanyu.mall.common.Constant;
import com.fanyu.mall.exception.FanyuMallException;
import com.fanyu.mall.exception.FanyuMallExceptionEnum;
import com.fanyu.mall.model.request.AddProductReq;
import com.fanyu.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

@RestController
public class ProductAdminController {
@Autowired
ProductService productService;
@PostMapping("admin/product/add")
public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq req){
productService.addProduct(req);
return ApiRestResponse.success();
}
@PostMapping("admin/upload/file")
public ApiRestResponse upload(HttpServletRequest request,@RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.indexOf("."));
//生成文件名称UUID
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;

//创建文件
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
if(!fileDirectory.exists()){
if(!fileDirectory.mkdir()){
throw new FanyuMallException(FanyuMallExceptionEnum.MKDIR_FAILED);
}
}
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}

try {
return ApiRestResponse.success(getHost(new URI(request.getRequestURL() + "")) + "/images/" + newFileName);
} catch (URISyntaxException e) {
return ApiRestResponse.error(FanyuMallExceptionEnum.UPLOAD_FAILED);
}
}

private URI getHost(URI uri){
URI effectiveURI;
try {
effectiveURI = new URI(uri.getScheme(),uri.getUserInfo(),uri.getHost(),uri.getPort(),null,null,null);
} catch (URISyntaxException e) {
effectiveURI = null;
}
return effectiveURI;
}
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,同学的项目是设置了项目名的,如:

http://img1.sycdn.imooc.com//climg/606fc60d0970c45505080099.jpg

建议同学在图片的访问路径中也加上项目名再试试。

祝学习愉快~

好帮手慕阿满 2021-04-08 16:54:58

同学你好,同学是图片已经上传成功了,但是无法通过浏览器访问。建议同学将如下红框中的内容修改为"file:" + Constant.FILE_UPLOAD_DIR再试试,如:

http://img1.sycdn.imooc.com//climg/606ec4a709ecd18405650223.jpg

修改后:

http://img1.sycdn.imooc.com//climg/606ec4dc09f2271c08770268.jpg

祝学习愉快~

  • 提问者 cloudonthesun #1

    老师您好,试了还是不行

    2021-04-08 16:58:52
  • 好帮手慕阿满 回复 提问者 cloudonthesun #2

    同学你好,建议同学删除target文件,清空浏览器的缓存,重启项目再试试。如果浏览器中还是无法访问图片,建议同学查看一下浏览器控制台,IDEA控制台等是否有报错,如果有报错,请将报错信息提供一下。

    祝学习愉快~

    2021-04-08 18:06:18
  • 提问者 cloudonthesun 回复 好帮手慕阿满 #3

    idea控制台无报错,

    http://img1.sycdn.imooc.com//climg/606ef45a0901870e23170908.jpg


    浏览器控制台报错如下http://img1.sycdn.imooc.com//climg/606ef3f7092a22e221091113.jpg

    2021-04-08 20:17:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师