【无标题】

文件上传和文件下载回显

package com.jing.reggie_takeout.controller;

import com.jing.reggie_takeout.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/common")
public class UploadDownloadController {
	// 从application.yaml中获取路径位置 
    @Value("${reggie.path}")
    private String basePath;


	 /**
     * 文件上传,传个MultipartFile对象,从该对象获取文件上传的信息
     * @param file
     * @return
     */
    @PostMa
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){


        // 原文件名
        String originalFilename = file.getOriginalFilename();

        // UUID文件名
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        // 创建当前日期的文件夹名
//        File file1 = new File(basePath+"\\"+ LocalDate.now());
        File file1 = new File(basePath);
        // 不存在。创建
        if (!file1.exists()){
            file1.mkdirs();
        }

        try {
//            file.transferTo(new File(basePath+"\\"+ LocalDate.now()+"\\"+  fileName));
            file.transferTo(new File(basePath+"\\"+  fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return Result.success(fileName);
    }


	/**
     * 文件下载
     * @param fileName
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("name") String fileName, HttpServletResponse response){
//        String name = basePath + "\\" + LocalDate.now() + "\\" + fileName;
        String name = basePath+ "\\" + fileName;
        FileInputStream fileInputStream = null;
        ServletOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(name));
            fileOutputStream = response.getOutputStream();
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                fileOutputStream.write(bytes,0,len);
                fileOutputStream.flush();
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }


}
server:
  port: 8080

spring:
  application:
    # \u5E94\u7528\u540D\u79F0\uFF0C\u53EF\u9009\uFF08\u4E0D\u586B\u5199\uFF0C\u4E3A\u9879\u76EE\u5DE5\u7A0B\u540D\uFF09
    name: reggie_take_out
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/reggie?useSSL=false&characterEncoding=UTF8&serverTimezone=UTC

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # \u9A7C\u5CF0\u547D\u540D
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: ASSIGN_ID

  type-aliases-package: com.jing.reggie_takeout.entity
  
reggie:
  path: H:\java_projects\reggie_takeout\src\main\resources\reggie\img\图片资源
<div>
          <el-form-item
            label="菜品图片:"
            prop="region"
            class="uploadImg"
          >
            <el-upload
              class="avatar-uploader"
              action="/common/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :on-change="onChange"
              ref="upload"
            >
              <img
                v-if="imageUrl"
                :src="imageUrl"
                class="avatar"
              ></img>
              <i
                v-else
                class="el-icon-plus avatar-uploader-icon"
              ></i>
            </el-upload>
          </el-form-item>
        </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值