application/json 参数读取排序

本文介绍了一种从HTTP请求中读取并解析数据的方法。通过将输入流转换为字节数组,然后使用默认编码将其转换为字符串,最后利用Json.NET进行反序列化,将请求体中的JSON数据转换为动态对象,并存储到字典中。
byte[] byts = new byte[HttpContext.Current.Request.InputStream.Length];
            HttpContext.Current.Request.InputStream.Read(byts, 0, byts.Length);
            string req = System.Text.Encoding.Default.GetString(byts);

            var te= Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(req);
            Dictionary<string, dynamic> dic = new Dictionary<string, dynamic>();
            foreach (var VARIABLE in te)
            {
                dic.Add(VARIABLE.Name, VARIABLE.Value);
            }

            foreach (var VARIABLE in dic.OrderBy(x => x.Key))
            {
                int i = VARIABLE.Value;
            }

package com.shunyu.yiwu.ai.platform.controller; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletResponse; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.http.*; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Objects; /** * 文件下载控制器 */ @RestController @RequestMapping("/file/download") @Tag(name = "文件下载", description = "处理文件下载相关接口") public class FileDownloadController { private final ResourcePatternResolver resourcePatternResolver; public FileDownloadController(ResourcePatternResolver resourcePatternResolver) { this.resourcePatternResolver = resourcePatternResolver; } /** * 下载帮助文档(默认返回目录下第一个文件) */ @GetMapping("/help-doc") public ResponseEntity<byte[]> downloadHelpDoc() { try { // 获取help-docs目录下的所有文件 Resource[] resources = resourcePatternResolver.getResources("classpath:help-docs/*"); if (resources.length == 0) { return ResponseEntity.notFound().build(); } // 获取第一个文件(按文件名排序以确保一致性) List<Resource> resourceList = Arrays.asList(resources); resourceList.sort((r1, r2) -> { try { return Objects.requireNonNull(r1.getFilename()).compareTo(Objects.requireNonNull(r2.getFilename())); } catch (Exception e) { return 0; } }); Resource fileResource = resourceList.get(0); String filename = fileResource.getFilename(); // 读取文件内容 byte[] fileContent; try (InputStream inputStream = fileResource.getInputStream()) { fileContent = inputStream.readAllBytes(); // 使用更可靠的读取方式 } // 检测内容类型 String contentType = detectContentTypeFromName(filename); // 对于Office Open XML格式文件,使用通用二进制流类型以避免编码问题 if (filename != null && (filename.toLowerCase().endsWith(".docx") || filename.toLowerCase().endsWith(".xlsx") || filename.toLowerCase().endsWith(".pptx"))) { contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; } // 对文件名进行UTF-8编码 String encodedFileName = null; if (filename != null) { encodedFileName = URLEncoder.encode(filename, StandardCharsets.UTF_8) .replace("+", "%20"); } // 构建响应 return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName) .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) .header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate") .header(HttpHeaders.PRAGMA, "no-cache") .header(HttpHeaders.EXPIRES, "0") .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileContent.length)) .body(fileContent); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } /** * 根据文件名检测Content-Type */ private String detectContentTypeFromName(String filename) { if (filename == null) { return MediaType.APPLICATION_OCTET_STREAM_VALUE; } String fileName = filename.toLowerCase(); if (fileName.endsWith(".pdf")) { return "application/pdf"; } else if (fileName.endsWith(".doc")) { return "application/msword"; } else if (fileName.endsWith(".docx")) { return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; } else if (fileName.endsWith(".xls")) { return "application/vnd.ms-excel"; } else if (fileName.endsWith(".xlsx")) { return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } else if (fileName.endsWith(".ppt")) { return "application/vnd.ms-powerpoint"; } else if (fileName.endsWith(".pptx")) { return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; } else if (fileName.endsWith(".txt")) { return "text/plain; charset=UTF-8"; } else if (fileName.endsWith(".html") || fileName.endsWith(".htm")) { return "text/html; charset=UTF-8"; } else if (fileName.endsWith(".css")) { return "text/css; charset=UTF-8"; } else if (fileName.endsWith(".js")) { return "application/javascript"; } else if (fileName.endsWith(".json")) { return "application/json; charset=UTF-8"; } else if (fileName.endsWith(".xml")) { return "application/xml; charset=UTF-8"; } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) { return "image/jpeg"; } else if (fileName.endsWith(".png")) { return "image/png"; } else if (fileName.endsWith(".gif")) { return "image/gif"; } else if (fileName.endsWith(".svg")) { return "image/svg+xml"; } else if (fileName.endsWith(".zip")) { return "application/zip"; } else if (fileName.endsWith(".rar")) { return "application/vnd.rar"; } else if (fileName.endsWith(".7z")) { return "application/x-7z-compressed"; } else { return MediaType.APPLICATION_OCTET_STREAM_VALUE; } } }以上的代码经过我的下载测试,我发现了下载的文件是docx格式会出现乱码现象,下载pdf格式会出现空白页面,下载xlsx格式跟源文件保持一致,下载txt格式跟源文件保持一致. 请进行解决
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值