记录 若依分离版图片上传至本地,下载和显示

目录

一、文件上传

 1、演示效果

2、后端代码

3、前端代码

二、文件下载

1、演示效果

2、后端代码

3、前端代码

三、显示图片

1、演示效果

2、后端代码

3、前端代码


以下为个人理解有错误的地方欢迎指出

一、文件上传
 1、演示效果

多选框选中一行

选择图片

上传成功

刷新

2、后端代码

保存的地址是当前项目的根目录下面的file/upload/id文件夹

@PostMapping("/addPartsUpload")
    public AjaxResult addPartsUpload(@RequestParam("id")Long id ,MultipartFile file) {
        Map<String,Object> map=new HashMap<>();
        System.err.println("id:"+id);
        System.err.println("file:"+file.getOriginalFilename());
        String filePath=System.getProperty("user.dir")+"/file/upload/"+id+"/";
        try {
            if (file.isEmpty()) {
                return AjaxResult.error("文件为空");
            }

            String fileName = System.currentTimeMillis()+"-"+file.getOriginalFilename();
            //文件上传的路径(当前项目的根目录)

            System.err.println(filePath);
            // 创建目标目录(如果不存在)
            File directory = new File(filePath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            // 保存文件到目标目录
            File uploadFile = new File(directory.getAbsolutePath() + File.separator + fileName);
            file.transferTo(uploadFile);
            String pathFan=filePath.replace("\\","/");
            //filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到

            System.err.println("替换后:"+pathFan);
            map.put("quote_address",pathFan+fileName);
            map.put("id",id);
            //修改数据库
            momOfferService.uploadImage(map);
            return AjaxResult.success("文件上传成功",pathFan+fileName);
        } catch (IOException e) {
             return AjaxResult.error("文件上传失败: "+e);
        }

    }
3、前端代码

上传按钮

 <el-upload
            class="upload-demo"
            :disabled="single"
            ref="upload"
            accept=".png, .jpg"
            :action="uploadAction"
            :file-list="fileListVideo"
            :headers="headers1"
            :before-upload="fieldBeforeUpload"
            :on-success="successUpload"
            :on-change="handleChangePhoto"
            :auto-upload="false">
          <el-button slot="trigger" size="small" :disabled="single" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
        </el-upload>

其他属性可以查看Element UI官网

https://element.eleme.cn/#/zh-CN/component/upload

调后端的js

好像这个uploadAction可以不用加(我是小白),这样应该可以上传文件了(如果局域网内其他电脑要访问,就要使用本机的ip地址,使用localhost其他电脑访问不了)

二、文件下载
1、演示效果

2、后端代码

在浏览器上下载:前端传一个文件地址过来

 @GetMapping("/download")
    public void download(@RequestParam String filePath,HttpServletResponse response) throws IOException {
        System.err.println("下载文件地址:"+filePath);
        // 获取要下载的文件
        File file = new File(filePath);

        // 设置响应头信息
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        response.setContentLength((int) file.length());

        // 读取文件并写入响应流
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = response.getOutputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
3、前端代码

 <el-button
            size="mini"
            type="text"
            title="附件下载"
            icon="el-icon-download"
            @click="download(scope.row)"
            v-hasPermi="['mom:offer:download']"
          ></el-button>

点击事件直接跳到后端(如果局域网内其他电脑要访问,就要使用本机的ip地址,使用localhost其他电脑访问不了)

三、显示图片
1、演示效果

2、后端代码

别人的都是直接使用下载的controller就能显示,我的就只是且只能下载,所以我下载和显示分开写的

@GetMapping("/fileView")
    public void fileView(@RequestParam String filePath,HttpServletResponse response) throws IOException {
        System.err.println("显示图片地址:"+filePath);
        // 获取要下载的文件
        File file = new File(filePath);
        if (!file.exists()){
            return;
        }
        // 设置响应头信息
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
        response.setContentLength((int) file.length());

        // 读取文件并写入响应流
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = response.getOutputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
3、前端代码

<el-table-column label="图片" align="center" prop="quote_address" show-overflow-tooltip >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="fileViewAction+scope.row.quote_address"
            :preview-src-list="[fileViewAction+scope.row.quote_address]"
          >
          </el-image>
        </template>
      </el-table-column>

(如果局域网内其他电脑要访问,就要使用本机的ip地址,使用localhost其他电脑访问不了)

大家看不懂可以去看看武哥的这个视频

【带小白做毕设08】手把手教Springboot+Vue实现文件上传和下载_哔哩哔哩_bilibili

依前后分离Linux部署是一种将前端和后端的服务分开部署的方式。通常情况下,前端指的是用户界面,可以是网页、移动应用或者桌面应用,而后端则是用户界面背后的服务器端应用程序。 在这种部署方式中,前端和后端可以独立部署和运行,它们之间通过网络通信进行交互。这种分离的部署方式更容易实现各自的扩展和升级,有效降低了系统的耦合度,提高了系统的可维护性和灵活性。 首先,我们需要选择合适的前端技术,比如React.js、Vue.js等,根据项目的需求和规模来选择合适的技术栈。 接着,我们可以使用Nginx或者Apache等web服务器来部署前端服务,同时配置反向代理将请求转发到后端服务的API接口。 对于后端服务,我们可以选择合适的后端框架进行开发,如Spring Boot、Express.js等,根据业务需求来构建相应的后端逻辑。 最后,我们需要将后端服务部署到独立的服务器上,并且配置数据库、缓存等其他相关的服务。通过这种方式,我们可以实现前后端分离的部署,使得系统更加灵活和可维护。 在整个部署过程中,我们还需要考虑系统的监控、日志记录、错误处理等一系列运维工作,确保系统的稳定和安全。通过前后分离Linux部署,我们可以更好地实现系统的模块化和分布式部署,提高系统的性能和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值