首页 > 数据库 >springboot上传资源到本地,数据库中存url

springboot上传资源到本地,数据库中存url

时间:2023-02-02 18:22:36浏览次数:38  
标签:springboot url System springframework file org println import 中存

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import dao.upAnddownDao;
import po.Sdownload;

@RestController
@RequestMapping("/test")
public class textController {

    @Autowired
    private upAnddownDao uad;

    // 上传
    @PostMapping("/upload")
    // 必须要有MultipartFile类型参数。而且参数名必须与表单file控件名一致
    public String upload(MultipartFile file) {
        // 上传图片存储目录
        String path = "d:/upload";
        // 获取文件名并使用UUID生成新文件名
        System.out.println(file);
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        System.out.println(fileName.substring(fileName.lastIndexOf(".")));
        String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        // 在指定上传图片存储目录中创建新文件
        File targetFile = new File(path, newFileName);
        // 如果找不到指定目录和文件,就新创建此目录和文件
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 将文件写入硬盘(myFile在内存中)
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Sdownload dl = new Sdownload();
        String url = path + "/" + newFileName;
        System.out.println(url);
        dl.setUrl(url);
        dl.setDeltag(0);
        Integer result = uad.upload(dl);
        System.out.println(result);

        return "ok";
    }

    // 下载
    @RequestMapping(value = "/down", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> downfile(Integer dlid) throws IOException {
        System.out.println(dlid);
        Sdownload sdl = uad.download(dlid);
        String url = sdl.getUrl();

        // 明确要读取的文件
        File file = new File(url);
        FileSystemResource fsr = new FileSystemResource(file);

        // 设置响应输出流的头部信息
        HttpHeaders hh = new HttpHeaders();
        String fname = URLEncoder.encode(file.getName(), "utf-8");
        System.out.println(fname);
        hh.add("Content-Disposition", "attachment;filename=\"" + fname + "\"");
        hh.add("Cache-Control", "no-cache, no-store, must-revalidate");
        hh.add("Pragma", "no-cache");
        hh.add("Expires", "0");

        // 完成响应输出
        return ResponseEntity.ok().headers(hh).contentLength(fsr.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream;charset=utf-8"))
                .body(new InputStreamResource(fsr.getInputStream()));

    }

}

 

标签:springboot,url,System,springframework,file,org,println,import,中存
From: https://www.cnblogs.com/liweimingbk/p/17086966.html

相关文章

  • SpringBoot Test - 典型的Springboot test注解说明
     重点汇总1.一个典型的springboottest的class写法: 2.@RunWith(SpringRunner.class)@RunWith,就是一个运行期,顾名思义就是“在XX环境下运行”。@RunWith(JUnit4.c......
  • @SpringBootApplication注解祥解
    @SpringBootApplication注解的详细分析 @SpringBootConfiguration:声明当前是一个配置类@ComponentScan:指出扫描进容器的文件 @EnableAutoConf......
  • Servlet-urlpartten配置和HTTP-概述
    Servlet-urlpartten配置Servlet相关配置1.urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})......
  • axios封装+根据环境请求不同的url
    封装axios的时候,会创建一个axios实例,然后配置他的baseUrl,今天搞了好久才搞定,特来记录下首先安装axiosnpminstallaxios然后新建文件夹utils,建个js文件,用来封装axio......
  • SpringBoot下动态数据源
    第一种:Mybatis-Plus的dynamic-datasourceGitee地址:https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter要实现其实很简单,一个注解就可以了1、创建......
  • vue.js客服系统实时聊天项目开发(十七)解决url get传参后进行base64解密问题
    有些参数需要在url的GET里传递,但是为了防止特殊字符问题,我转成了base64编码。但是js进行解码的时候,总是报错:报错:Failedtoexecute'atob'on'Window':Thestringto......
  • SpringBoot配置jdk11
    最近想尝试用jdk11跑一下SpringBoot,在网上找了篇还不错的博客,​​地址​​,jdk11有很多不错的改进,因此有必要把万年不换的jdk8给换一下修改properties替换mavn插件<plugin>......
  • Springboot数据库配置文件加密
    引入工具依赖包:!--配置文件加密--><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>1.......
  • JavaScript 中URL构造函数
    前言URL对于我们开发人员来讲,应该是非常熟悉了。在对URL进行参数拼接时,我们一般都会直接进行字符串拼接或使用模版字符串,因为这样非常方便,但是我们这样其实会在不知不觉......
  • SpringBoot框架下shiro与jwt的结合的用户登录
    写了一次使用shiro和jwt的用户登录(没有涉及到用户权限的控制),下面进行很简单的技术总结之前使用的是Redis中保存用户信息,使用uuid-用户信息的键值对来判断用户......