java文件/图片的上传与下载 以及MultipartFile

一、概述

    MultipartFile为org.springframework.web.mutipart包下的一个类,也就是说如果想使用MultipartFile这个类就必须引入spring框架,换句话说,如果想在项目中使用MultipartFile这个类,那么项目必须要使用spring框架才可以,否则无法引入这个类。MultipartFile翻译成中文来讲就是“多组件的文档”,不用太在乎他的中文含义,一般来讲使用MultipartFile这个类主要是来实现以表单的形式进行文件上传功能。

二、MultipartFile中的方法

(1)、getName方法

  getName方法获取的是前后端约定的传入文件的参数的名称

(2)、getOriginalFileName方法

         getOriginalFileName方法获取的是文件的完整名称,包括文件名称+文件拓展名。

(3)、getContentType方法

         getContentType方法获取的是文件的类型,注意是文件的类型,不是文件的拓展名。
(4)、isEmpty方法
         isEmpty方法用来判断传入的文件是否为空,如果为空则表示没有传入任何文件。

(5)、getSize方法

         getSize方法用来获取文件的大小,单位是字节。

(6)、getBytes方法

         getBytes方法用来将文件转换成一种字节数组的方式进行传输,会抛出IOException异常。

(7)、getInputStream方法

         getInputStream方法用来将文件转换成输入流的形式来传输文件,会抛出IOException异常。

(8)、transferTo方法

         transferTo方法用来将接收文件传输到给定目标路径

三、MultipartFile的一些使用技巧

    (1)我们在使用MultipartFile作为参数传递的时候,可以将MultipartFile声明为一个数组,这样就能支持多文件传输,如果只需要传输一个文件,则去掉数组就好了。
    (2)可以根据MultipartFile的getSize方法来获取到传输文件的大小,这样就能限定传输过来的文件的大小了。

四、上传代码显示

业务逻辑代码:

package cn.xcy.demo.controller;

import cn.hutool.core.io.FileUtil;
import cn.xcy.demo.FlieUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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.IOException;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileUploadController {
    /**
     *  上传文件
     *  1、判断文件是否为空
     *  2、获取文件名、文件类型、文件大小(可以限定文件类型、以及文件大小)
     *  3、设置文件上传路径
     *  4、以流的方式将文件输出
     *  5、返回上传成功
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws Exception{
        // 判断文件是否为空
        if (file.isEmpty()) {
            return "上传失败";
        }
        //文件类型为
        String contentType = file.getContentType();
        log.info("文件类型为:" + contentType);
        if (contentType.equals("image/jpeg")){
            return "请上传正确格式的文件";
        }
        // 获取原始文件名
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名为:" + fileName);
        try {
            // 将文件保存到指定位置
            byte[] bytes = file.getBytes();
            log.info("文件大小为:" + bytes.length);
            // TODO: 根据业务需求选择合适的存储方式,比如保存到本地磁盘或云存储
//            获取生成后的文件名
            String s = FlieUtils.generateRandomFileName(fileName);
//            将文件保存到指定位置
            FlieUtils.upload(file,"C:\\Users\\liguoming\\Desktop",s);
            return "上传成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

}

FlieUtils工具类:

package cn.xcy.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;

@Slf4j
public class FlieUtils {
    /**
     * @param file     文件
     * @param fileName 新的随机文件名
     */
    public static void upload(MultipartFile file, String destPath, String fileName) {
        File dest = new File(destPath + File.separator + fileName);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(dest);
        } catch (Exception e) {
            log.info("Save file exception. {}", e.getMessage());
        }

    }

    /**
     * 输出文件的后缀名
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
//        fileName.substring(fileName.lastIndexOf("."))
//        是C#中的字符串操作,它的作用是从文件名字符串中截取最后一个点(.)及其后面的所有字符。这个操作可以用来获取文件的扩展名。
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 输出文件的新名称 (名字+后缀名)
     * @param fileName
     * @return
     */
    public static String generateRandomFileName(String fileName) {
        return UUID.randomUUID() + getSuffix(fileName);
    }

    /**
     * @param name
     * @Description 设置响应头部信息
     * @Throws
     * @Return java.lang.String
     * @Date 2023-08-02 13:39:15
     * @Author lgm
     */
    public static String fileContentType(String name) {
        String result = "";
        String fileType = name.toLowerCase();
        if (fileType.endsWith(".png")) {
            result = "image/png";
        } else if (fileType.endsWith(".gif")) {
            result = "image/gif";
        } else if (fileType.endsWith(".jpg") || fileType.endsWith(".jpeg")) {
            result = "image/jpeg";
        } else if (fileType.endsWith(".svg")) {
            result = "image/svg+xml";
        } else if (fileType.endsWith(".doc")) {
            result = "application/msword";
        } else if (fileType.endsWith(".xls")) {
            result = "application/x-excel";
        } else if (fileType.endsWith(".zip")) {
            result = "application/zip";
        } else if (fileType.endsWith(".pdf")) {
            result = "application/pdf";
        } else if (fileType.endsWith(".mpeg")) { //MP3
            result = "audio/mpeg";
        } else if (fileType.endsWith(".mp4")) {
            result = "video/mp4";
        } else if (fileType.endsWith(".plain")) {
            result = "text/plain";
        } else if (fileType.endsWith(".html")) {
            result = "text/html";
        } else if (fileType.endsWith(".json")) {
            result = "application/json";
        } else{
            result = "application/octet-stream";
        }
        return result;
    }
}

五、下载的代码显示

业务逻辑代码:

/**
     *  下载文件
     *  1、需要传入文件名称
     *  2、先将文件的存放路径+名称获取到
     *  3、设置响应头,告诉浏览器需要下载文件夹
     *  4、设置响应体,将文件读取出来,写入到响应体中
     *  5、response.getOutputStream()  输出字节流, 客户端会下载字节流并生成文件保存本地
     * @param filename
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/fileload")
    public String fileload(@RequestParam("filename")String filename, HttpServletResponse response) throws IOException {
        // 文件的存放位置
        String filePath = "C:\\Users\\liguoming\\Desktop\\" + filename;
        File file = new File(filePath);
        // 设置响应头,告诉浏览器要下载文件
        // response.setContentType()是设置响应类型的 浏览器读取的是什么文件 依据文件类型,告诉浏览器接下来要做的是什么
        // application/octet-stream  告诉浏览器需要下载二进制流数据(如常见的文件下载)
        response.setContentType("application/octet-stream");
        /**
         * response.setHeader("Content-Disposition", "attachment; filename=" +new String( filename.getBytes("gb2312"), "ISO8859-1" ));
         * 在确保附件文件名都是简体中文字的情况下,那么这个办法确实是最有效的,不用让客户逐个的升级IE。
         * 如果台湾同胞用,把gb2312改成big5就行。但现在的系统通常都加入了 国际化的支持,普遍使用UTF-8。
         * 如果文件名中又有简体中文字,又有繁体中文,还有日文。那么乱码便产生了。另外,在上Firefox (v1.0-en)下载也是乱码。
         */
        response.setHeader("Content-Disposition", "attachment; filename=" +new String( filename.getBytes("gb2312"), "ISO8859-1" ));
        //response.getOutputStream()  输出字节流, 客户端会下载字节流并生成文件保存本地
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(FileUtil.readBytes(file));
        outputStream.close();
        return "success";
    }

  • 15
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
send函数和recv函数是VC++中Socket编程中最常用的两个函数,它们分别用于发送数据和接收数据。 send函数的原型如下: ```c++ int send( SOCKET s, const char* buf, int len, int flags ); ``` 参数说明: - s:需要发送数据的Socket。 - buf:指向包含要发送数据的缓冲区的指针。 - len:要发送的数据的字节数。 - flags:调用方式标志。 返回值说明: - 成功:返回实际发送的字节数。 - 失败:返回SOCKET_ERROR。 recv函数的原型如下: ```c++ int recv( SOCKET s, char* buf, int len, int flags ); ``` 参数说明: - s:需要接收数据的Socket。 - buf:指向接收数据的缓冲区的指针。 - len:要接收的数据的最大字节数。 - flags:调用方式标志。 返回值说明: - 成功:返回实际接收的字节数。 - 失败:返回SOCKET_ERROR。 send和recv函数的使用方法如下: ```c++ char sendbuf[] = "Hello, world!"; int sendbuflen = sizeof(sendbuf); int sentbytes = 0; sentbytes = send(sock, sendbuf, sendbuflen, 0); if (sentbytes == SOCKET_ERROR) { // 发送失败 } char recvbuf[1024]; int recvbuflen = sizeof(recvbuf); int receivedbytes = 0; receivedbytes = recv(sock, recvbuf, recvbuflen, 0); if (receivedbytes == SOCKET_ERROR) { // 接收失败 } ``` 使用send和recv函数时需要注意以下几点: - send和recv函数都是阻塞式的,即程序会一直等待直到发送或接收完所有数据。 - 如果发送或接收的数据量比较大,需要多次调用send或recv函数。 - 如果发送或接收的数据量比较小,建议使用TCP_NODELAY选项关闭Nagle算法,以提高发送和接收的效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

越过难题

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

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

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

打赏作者

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

抵扣说明:

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

余额充值