springboot上传下载文件

@RequestMapping("bigJson")

@RestController

@Slf4j

public class TestBigJsonController {

复制代码
@Resource
private BigjsonService bigjsonService;

@PostMapping("uploadJsonFile")
public ResponseResult<Long> uploadJsonFile(@RequestParam("file")MultipartFile file){
    if (file.isEmpty()) {
        return ResponseResult.error();
    }

    try {
        Bigjson bigjson = new Bigjson();
        bigjson.setJsonFileName(file.getName());

        // 解析JSON文件
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject jsonObject = objectMapper.readValue(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8), JSONObject.class);
        bigjson.setJsonFile(jsonObject.toJSONString());

        //
        Long id = bigjsonService.uploadJsonFile(bigjson);

        // 返回成功响应
        return ResponseResult.success(id);
    } catch (IOException e) {
        log.error("上传错误");
        return ResponseResult.error();
    }
}

@GetMapping("/downloadJson")
public ResponseEntity<byte[]> downloadJson(@RequestParam("id") Long id){
    Bigjson bigjson = bigjsonService.getById(id);
    byte[] bytes = bigjson.getJsonFile().getBytes();

    // 设置响应头
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+bigjson.getJsonFileName()+".json");
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");

    // 返回响应
    return new ResponseEntity<>(bytes, headers, HttpStatus.OK);

}

}

相关推荐
平安的平安10 小时前
传统Java工程师第一次用飞算JavaAI生成SpringBoot项目
java
csjane107910 小时前
Redisson 限流原理
java·redis
一个做软件开发的牛马10 小时前
MyBatis 从零实战:完整搭建可运行 Demo,注解与 XML 双模式开发详解
java·后端
用户2986985301410 小时前
Java 实践:查找与提取 Word 文档超链接
java·后端
Flittly10 小时前
【AgentScope Java新手村系列】(9)SpringBoot集成
java·spring boot·spring
星环科技10 小时前
数据标准Agent ,让企业数据说同一种语言
java·开发语言·前端
SimonKing10 小时前
Qoder 提供免费 Qwen3.7-Max,无需订阅
java·后端·程序员
杨运交10 小时前
[033][缓存模块]基于 Redisson 的租户隔离 Redis Key 前缀设计
spring boot
dadaobusi11 小时前
RISC-V 虚拟化:虚拟机TLB处理
java·开发语言
程序猿乐锅11 小时前
【 苍穹外卖day03 | 菜品管理 】
java·开发语言·数据库·mysql