0
点赞
收藏
分享

微信扫一扫

关于上传文件到static目录下需要重启项目

像小强一样活着 2022-03-30 阅读 51
java

图片上传:

import com.pws.bookshop.util.ResultCode;
import com.pws.bookshop.util.ResultVO;
import com.pws.bookshop.exception.CustomizeException;
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 javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Description: 图片上传控制器
 * @author Wen先森
 * @version 1.0
 * @date 2022/3/5 16:49
 */
@RestController
@RequestMapping("/upload")
public class ImageUploadController {

    @PostMapping("/book_image")
    public ResultVO uploadBookImage(MultipartFile bookImage, HttpServletRequest request) {

        //获取项目上传文件夹路径
        String path = "src/main/resources/static/images/book_images";
        File pathFile=new File("src/main/resources/static/images/book_images");
        System.out.println(pathFile.getAbsolutePath());
        String targetFileName=bookImage.getOriginalFilename();
        File targetFile = new File(pathFile.getAbsolutePath(), targetFileName);

        //上传
        try {
            bookImage.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomizeException(ResultCode.FAILED, "上传图片失败");
        }
        return new ResultVO(ResultCode.SUCCESS,targetFileName);
    }
}

在这里插入代码片上传接口前端
页面

<div class="layui-form-item">
                    <label class="layui-form-label">上传图片</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="upload-book-image">
                            <i class="layui-icon">&#xe67c;</i>上传图片
                        </button>
                        <input type="text" id="image" name="image" autocomplete="off" class="layui-input" style="display: none;">
                    </div>
                </div>

js

 //上传图片
        var uploadInst = upload.render({
            elem: '#upload-book-image',//绑定元素
            url: '/upload/book_image',//上传接口
            accept: 'images',//只允许上传图片
            acceptMime: 'image/*',//只筛选图片
            size: 1024*2 , //限定大小
            field:'bookImage',
            done: function (res) {
                //上传完毕回调
                if(res.code!=0){
                    return layer.msg(res.msg,{icon:2});
                }
                $("#image").val(res.data);
                return layer.msg("上传成功",{icon:1});
            },
            error: function () {
                //请求异常回调
            }
        });

因为上传图片到static目录下需要重启项目所以要用拦截器

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取文件的真实路径
        String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\book_images";
        //       /images/**是对应resource下工程目录
        registry.addResourceHandler("/images/**").addResourceLocations("file:"+path);
    }
}
举报

相关推荐

0 条评论