zoukankan      html  css  js  c++  java
  • 实现Java客户端上传文件与Java服务端接收文件

    Java客户端通过HTTP协议上传文件, 服务端处理客户端请求, MultipartFile转File, 实现客户端上传文件的存储

    Java环境: JDK1.8
    开发环境: IDEA
    SpringBoot: 2.2.0
    Maven: 3.6.3

    Java客户端通过HTTP协议上传文件

    // 引入pom依赖, hutool相关文档, https://www.hutool.cn/docs/
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.3.7</version>
    </dependency>
    
    HashMap<String, Object> paramMap = new HashMap<>();
    //文件上传只需将参数中的键指定(默认file),值设为文件对象即可,对于使用者来说,文件上传与普通表单提交并无区别
    paramMap.put("file", FileUtil.file("C:\文件路径\文件名称"));
    
    String result = HttpUtil.post("服务端IP:端口", paramMap);
    

    Java服务端接收请求并实现文件的存储
    工具类

    public class Utils {
        public static void saveFile( MultipartFile filecontent){
            OutputStream os = null;
            InputStream inputStream = null;
            String fileName = null;
            try {
                inputStream = filecontent.getInputStream();
                fileName = filecontent.getOriginalFilename();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                String path = "C:\test\";
                // 2、保存到临时文件
                // 1K的数据缓冲
                byte[] bs = new byte[1024];
                // 读取到的数据长度
                int len;
                // 输出的文件流保存到本地文件
                File tempFile = new File(path);
                if (!tempFile.exists()) {
                    tempFile.mkdirs();
                }
                os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
                // 开始读取
                while ((len = inputStream.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 完毕,关闭所有链接
                try {
                    os.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    Controller类

    @Controller
    public class FileController {
        @RequestMapping("/")
        @ResponseBody
        public String index(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
            Utils.saveFile(file);
            return "Success";
        }
    }
    

    注意:

    文件较大时spring-boot 服务端报上传文件错误“spring.servlet.multipart.max-file-size”
    可以修改配置文件: application.properties, 添加以下配置..大小自行修改...

    spring.servlet.multipart.max-file-size=200MB
    spring.servlet.multipart.max-request-size=200MB

  • 相关阅读:
    linux 命令——48 watch (转)
    linux 命令——47 iostat (转)
    linux 命令——46 vmstat(转)
    linux 命令——45 free(转)
    linux 命令——44 top (转)
    linux 命令——43 killall(转)
    linux 命令——42 kill (转)
    linux 命令——41 ps(转)
    linux 命令——40 wc (转)
    Java for LeetCode 068 Text Justification
  • 原文地址:https://www.cnblogs.com/hellomrr/p/13237711.html
Copyright © 2011-2022 走看看