springboot 使用总结-持续更新……

1, springboot 文件上传到某个位置,使用虚拟位置进行访问

import org.springboot.sample.interceptor.MyInterceptor1;
import org.springboot.sample.interceptor.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
registry.addResourceHandler("/myimgs/aaa/**").addResourceLocations("file:H:/myimgs/");
super.addResourceHandlers(registry); } }

2,springboot  liunx 后台启动,并且指定日志输出位置。

nohup java -jar guns-admin-1.0.0.jar > catalina.out  2>&1 &

3,springboot上传文件

 /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public JSONObject upload(@RequestPart("image") MultipartFile picture,HttpServletRequest request) {

        String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
        try {
            String fileSavePath = "ssssssss";
            picture.transferTo(new File(fileSavePath + pictureName));
        } catch (Exception e) {
            e.printStackTrace();
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }
        JSONObject json  = new JSONObject();
        json.put("errno", 0);
        JSONArray data = new JSONArray();
        data.add("/static/images/"+pictureName);
        json.put("data", data);
        return json;
    }

猜你喜欢

转载自www.cnblogs.com/myhanghang/p/10390254.html