首页 > 其他分享 >文件上传和下载

文件上传和下载

时间:2023-04-02 11:13:54浏览次数:27  
标签:文件 uuid web springframework new import 上传 下载 String

 

 

 

 

 

 

 

 

import com.rome.backend.common.R;
import org.springframework.web.bind.annotation.GetMapping;
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.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传下载
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    /**
     * 上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file){//参数名和前端input名一样
        //file是一个临时文件,需要转存到指定位置,否则本次请求完成后临时文件会删除
        //获取当前项目路径进行拼接
       String url = System.getProperty("user.dir")+"\\backend\\src\\main\\resources\\backend\\images\\dish\\dishload\\";
       //使用uuid重新生成文件名防止文件名重复造成覆盖,uuid+原始文件名
        String uuid = UUID.randomUUID().toString();
//        uuid+原始文件名
        String fileName = uuid.concat(file.getOriginalFilename());

//        将文件转存
        try {
            file.transferTo(new File(url+fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.success(fileName);
    }

    /**
     * 下载
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        //图片路径
        String urlImg = System.getProperty("user.dir")+"\\backend\\src\\main\\resources\\backend\\images\\dish\\dishload\\".concat(name);
        try {
            //通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(urlImg));
            

            //输出流到浏览器

            ServletOutputStream outputStream = response.getOutputStream();

            //设置响应回去的是什么类型的文件
            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();

        } catch (Exception e) {
           e.printStackTrace();
        }
      
    }
}

  

标签:文件,uuid,web,springframework,new,import,上传,下载,String
From: https://www.cnblogs.com/romablog/p/17280076.html

相关文章

  • 文件操作
    /***拷贝文件*/publicstaticvoidcopyFile(FilesrcFile,FiletargetFile)throwsIOException{FileInputStreaminputStream=newFileInputStream(srcFile);FileOutputStreamoutputStream=newFileOutputStream(targetFile);......
  • 将pdf文件以二进制形式拆分并且写入另一个pdf
    staticpublicvoidmain(String[]args){Filefile=newFile("C:\\ceshi2\\ceshi.pdf");Filefile2=newFile("C:\\ceshi2\\123.pdf");try{InputStreamInputStream=newFileInputStream(file);......
  • 本地Linux主机使用SFTP上传文件到Linux云服务器_弹性云服务器 ECS
    本地Linux主机使用SFTP上传文件到Linux云服务器_弹性云服务器ECS_文件上传_ (dawuzhe.cn)更新时间:2020/12/15GMT+08:00操作场景本节操作以CentOS操作系统为例,介绍配置SFTP、使用SFTP上传或下载文件、文件夹的操作步骤。 操作步骤以root用户登录云服务器。执行以......
  • 【转载】 解决VSCode运行或调试python文件无反应的问题
    ————————————————版权声明:本文为CSDN博主「姜行者」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/kraisi/article/details/127287966  ====================================================  ......
  • python pygame播放音频文件
    pythonpygame播放音频文件 pipinstallpygame  importpygame#Initializepygamepygame.init()#LoadtheMP3filepygame.mixer.music.load("1.mp3")#PlaytheMP3filepygame.mixer.music.play()#WaitfortheMP3filetofinishplayingwhil......
  • sql server 收缩日志文件
    在SQLServer中,可以使用DBCCSHRINKFILE命令来收缩数据库的事务日志文件。下面是一个示例:USE<database_name>GO--收缩日志文件DBCCSHRINKFILE(<log_file_name>,<target_size_in_MB>)其中,<database_name>是要操作的数据库名称,<log_file_name>是要收缩的事......
  • RabbitMQ下载与安装
    1.首先进入rabbitmq官网找,查看rabbitmq对应的erlang之间对应的版本信息网址:RabbitMQErlangVersionRequirements—RabbitMQ网站看不懂的话可以使用windows系统最新的edge浏览器,有翻译功能.2.在github网站下载erlang的.npm文件(文件下载可能较慢,耐心等待)网址:http......
  • maven打包后加载外部配置文件
    pom.xml文件配置在bulid中指定resources路径并指定不加载的配置文件<resources> <resource> <directory>src/main/resources</directory> <excludes> <!--<exclude>*.xml</exclude> <exclude>*.ini</exclude&g......
  • 将本地代码上传至Gitee
    1、将代码和README.md文件放在同一个文件夹下 2、在该文件夹下打开git窗口,并创建本地仓库//初始化本地仓库gitinit//查看本地仓库状态gitstatus 3、将项目提交到仓库gitadd--allgitcommit-m“SwaggerDemo” 4、将代码上传至Gitee//将Gitee上的仓库和......
  • ubuntu16 共享主机的文件夹(实测有效)
    1、主机VM软件开启共享文件夹1)进入:虚拟机》设置》选项》共享文件夹2)选择总是启用》点击添加》选择一个主机目录 3)保存目录》下一步》启用此共享》完成   2、ubuntu安装vmtools1)下载包:进入:虚拟机》重新安装vmtools  (如果是灰色的,进入虚拟机设置,启动项改为物理......