Java工作笔记-Spring Boot上传图片并显示

370 篇文章 20 订阅
140 篇文章 4 订阅

目录

 

 

基本概念

代码与实例

源码打包下载


 

 

基本概念

这个是基因Github的一个项目改的,因为页面关闭了,在此找不到了,不能把连接发上来。

这里使用的是FreeMarker。

为了避免重名,使用了UUID生成随机。

找图片文件主要是使用ResourceLoader。

 

代码与实例

程序运行截图如下:

点击选择文件,然后提交:

看看文件夹:

关键源码如下:

application.properties

### FreeMarker 配置
spring.freemarker.allow-request-override=false
#Enable template caching.启用模板缓存。
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#设置面板后缀
spring.freemarker.suffix=.ftl

# 设置单个文件最大内存
multipart.maxFileSize=50Mb
# 设置所有文件最大内存
multipart.maxRequestSize=50Mb
# 自定义文件上传路径
web.upload-path=F:/SpringTest/

TestController.java

package com.example.demo.controller;

import com.example.demo.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Controller
public class TestController {

    private final ResourceLoader resourceLoader;

    public TestController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Value("${web.upload-path}")
    private String path;

    @RequestMapping("test")
    private String toUpload(){

        return "test";
    }

    //上传文件
    @RequestMapping("fileUpload")
    public String upload(@RequestParam("fileName") MultipartFile file, Map<String, Object> map){

        String localPath = "F:/SpringTest";
        String msg = "";

        StringBuffer newName = new StringBuffer();
        if(FileUtils.upload(file, localPath, file.getOriginalFilename(), newName)){

            msg = "上传成功";
        }
        else{

            msg = "上传失败";
        }

        newName.toString();


        map.put("msg", msg);
        //map.put("fileName", file.getOriginalFilename());
        map.put("fileName",  newName.toString());

        return "forward:/test";
    }


    //显示单张图片
    @RequestMapping("show")
    public ResponseEntity showPhotos(String fileName){

        if(fileName == null){

            return ResponseEntity.notFound().build();
        }

        try{

            return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));
        }
        catch (Exception e){

            return ResponseEntity.notFound().build();
        }
    }
}

 

 

源码打包下载

地址如下:

https://github.com/fengfanchen/Java/tree/master/loadPic

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值