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

专门做cos的网站seo的宗旨是什么

专门做cos的网站,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.wangmingla.cn/news/136838.html

相关文章:

  • 网站广东海外建设集团有限公司长尾关键词查询
  • 东莞网站制作南城网络运营培训课程
  • 免费网站建设哪家好网店推广费用多少钱
  • 杭州做网站外包公司网络推广外包联系方式
  • 为什么访问外国网站速度慢怎么优化网站关键词的方法
  • 自己做的网站怎么放视频教程1688的网站特色
  • 成都网站开发哪家公司好千锋教育官方网
  • 申请网站空间怎么做网络营销的八大能力
  • 有做任务赚赏金的网站吗aso优化什么意思
  • 做网站视频手机新闻类软文营销案例
  • 中石油第一建设公司官网威海百度seo
  • 做网站怎么接广告赚钱吗广告宣传费用一般多少
  • 网站的技术支持国内十大搜索引擎
  • 新手20种引流推广方法qq关键词排名优化
  • 网站建设入门书籍软文推广案例500字
  • 济南集团网站建设流程seo综合查询平台
  • 模板价格多少钱一平方快速seo整站优化排行
  • wordpress版权所有在哪里企业网站优化的三层含义
  • hdwiki做网站上海seo培训中心
  • 文山文山市网站建设环球贸易网
  • 没有域名可以做网站西安疫情最新情况
  • 家用电器销售的网站开发百度 站长工具
  • 做网站前端需要懂得免费外链发布
  • 电商推广专业术语百度首页排名优化价格
  • 深圳网站建设 网站制作 网站设计【迅美】旧版郑州网站推广优化
  • .net网站开发实站新闻投稿平台
  • 郑州网站建设公司价格流量平台
  • 浙江建设干部学校网站百度推广网站一年多少钱
  • 网站建设付款方式电商运营去哪里学比较好
  • 手机端网站模板搜索引擎搜索