当前位置: 首页 > news >正文

网站的备案可以管几年海豹直播nba

网站的备案可以管几年,海豹直播nba,b2b网站建设解决方案,蚌埠seo公司现在有一个实体类对象的集合&#xff0c;需要将它们转换为xml文档&#xff0c;xml文档就是标签集合的嵌套&#xff0c;例如一个学生类&#xff0c;有姓名、年龄等&#xff0c;需要转换成一下效果&#xff1a; <student><age>14</age><name>张三</na…

现在有一个实体类对象的集合,需要将它们转换为xml文档,xml文档就是标签集合的嵌套,例如一个学生类,有姓名、年龄等,需要转换成一下效果:

 <student><age>14</age><name>张三</name></student><student><age>15</age><name>李四</name></student><student><age>16</age><name>王五</name></student>

首先定义student的实体类:

import lombok.Data;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;@Data
@XmlRootElement
public class Student {private String name;private int age;// 无参构造函数(必须有)public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}@XmlElementpublic String getName() {return name;}public void setName(String name) {this.name = name;}@XmlElementpublic int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

因为是student的集合,需要再构建一个StudentListWrapper类:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;@XmlRootElement
public class StudentListWrapper {private List<Student> students;public StudentListWrapper() {}public StudentListWrapper(List<Student> students) {this.students = students;}@XmlElement(name = "student")public List<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}
}

两种下载方式:无返回值的:

 @GetMapping("testXmlDownload")public void testXmlDownload(HttpServletResponse response) throws Exception {try {// 创建一个包含 Student 对象的集合List<Student> studentList = new ArrayList<>();studentList.add(new Student("张三", 14));studentList.add(new Student("李四", 15));studentList.add(new Student("王五", 16));// 创建 JAXB 上下文JAXBContext context = JAXBContext.newInstance(StudentListWrapper.class);// 创建 MarshallerMarshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 将集合序列化为 XMLStringWriter writer = new StringWriter();marshaller.marshal(new StudentListWrapper(studentList), writer);// 输出 XML 格式的文档String xmlDocument = writer.toString();byte[] bytes = xmlDocument.getBytes();// 使用 ByteArrayInputStream 将字节数组转换为 InputStreamInputStream inputStream = new ByteArrayInputStream(bytes);// 使用 BufferedInputStream 包装 InputStreamBufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);byte[] buffer = new byte[bufferedInputStream.available()];bufferedInputStream.read(buffer);bufferedInputStream.close();response.reset();response.setCharacterEncoding("UTF-8");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("student".concat(".xml"), "UTF-8"));//response.addHeader("Content-Length", "" + file.length());OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");outputStream.write(buffer);outputStream.flush();// System.out.println(xmlDocument);} catch (Exception e) {e.printStackTrace();}}

第二种:使用 ResponseEntity 对象来封装响应内容和响应头,并返回给客户端:

@GetMapping("/download")public ResponseEntity<byte[]> downloadFile() throws Exception {// 创建一个包含 Student 对象的集合List<Student> studentList = new ArrayList<>();studentList.add(new Student("张三", 14));studentList.add(new Student("李四", 15));studentList.add(new Student("王五", 16));// 创建 JAXB 上下文JAXBContext context = JAXBContext.newInstance(StudentListWrapper.class);// 创建 MarshallerMarshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 将集合序列化为 XMLStringWriter writer = new StringWriter();marshaller.marshal(new StudentListWrapper(studentList), writer);// 输出 XML 格式的文档String xmlContent = writer.toString();// byte[] bytes = xmlDocument.getBytes();// 将 XML 内容转换为字节数组byte[] xmlBytes = xmlContent.getBytes(StandardCharsets.UTF_8);// 构造响应头HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_XML);headers.setContentDispositionFormData("attachment", "students.xml");// 构造 ResponseEntity 对象,设置响应内容和响应头ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(xmlBytes, headers, HttpStatus.OK);return responseEntity;}

使用postman测试:
在这里插入图片描述

复制到浏览器测试:
在这里插入图片描述
打开效果:
在这里插入图片描述

http://www.hkea.cn/news/995041/

相关文章:

  • wordpress企业中文模板太原seo哪家好
  • 广东网广东网站建设网站推广方案模板
  • 网站运营知识快手seo
  • 咖啡公司网站建设策划书微信营销方式
  • 柳江区城乡住房建设局网站上海seo优化服务公司
  • 西城企业网站建设企业网站怎么优化
  • 初学者做动态网站项目例子游戏特效培训机构排名
  • 汽车类网站搭建直链平台
  • 做网站遇到的困难总结网络营销软件代理
  • 做网站登录论坛外链代发
  • 东营专业网站建设公司排行青岛谷歌优化公司
  • 公众号和网站先做哪个口碑营销的形式
  • 长沙企业建网站费用关键词搜索推广排行榜
  • 怎么做网站端口代理沧州网络推广外包公司
  • php wordpress 目录seo课程培训机构
  • 常州网站建设方案优化引流app推广软件
  • 网络营销网站建设实训网络营销步骤
  • 网站都有后台吗百度竞价开户公司
  • 秭归网站建设网站seo优化心得
  • wordpress电影网站模板seo运营
  • 公司注册网上核名业务如何终止网站排名优化怎么做
  • 网站建设伍金手指下拉2网上推广平台
  • 沧州网站建设公司翼马爱情链接
  • 计算机学了出来干嘛免费优化推广网站的软件
  • 宁波网站建设优化湖南seo优化按天付费
  • 门户网站手机版google官网入口
  • 深圳市工程建设交易服务中心网站软文什么意思
  • 大型网架加工厂成都网站建设方案优化
  • 导航网站的广告怎么做的千锋教育官方网
  • etc网站开发票网站制作软件免费下载