发票xml下载,xml中文乱码问题修复

new
JilingLee 1 year ago
parent fe44fcf1b8
commit bb051d32db

@ -90,6 +90,12 @@
<version>1.7.3-snapshot</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-setting-biz</artifactId>
<version>1.7.3-snapshot</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -10,6 +10,7 @@ import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.Converter;
import cn.iocoder.yudao.module.accounting.enums.AccountingStatusEnum;
import cn.iocoder.yudao.module.bs.utils.BaiduOcrHandler;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.setting.service.passwords.PasswordsService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import liquibase.pro.packaged.A;
@ -60,6 +61,8 @@ public class InvoicesController {
private InvoicesService invoicesService;
@Resource
private FileService fileService;
@Resource
private PasswordsService passwordsService;
@PostMapping("/create")
@Operation(summary = "创建发票")
@ -111,6 +114,16 @@ public class InvoicesController {
return success(InvoicesConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/downloadXml")
@Operation(summary = "获得发票分页Xml")
@PreAuthorize("@ss.hasPermission('accounting:invoices:query')")
public void downloadXml(@Valid InvoicesPageReqVO pageVO,HttpServletResponse response) throws IOException {
PageResult<InvoicesDO> pageResult = invoicesService.getInvoicesPage(pageVO);
PageResult<InvoicesRespVO> invoicesRespVOPageResult = InvoicesConvert.INSTANCE.convertPage(pageResult);
String jsonString = JSONObject.toJSONString(invoicesRespVOPageResult);
passwordsService.jsonToXmlConverter(response,pageVO.getInputPassword(),jsonString);
}
@GetMapping("/export-excel")
@Operation(summary = "导出发票 Excel")
@PreAuthorize("@ss.hasPermission('accounting:invoices:export')")

@ -208,4 +208,7 @@ public class InvoicesPageReqVO extends PageParam {
@Schema(description = "发票地址")
private String fileUrl;
@Schema(description = "密码")
String inputPassword;
}

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.setting.service.passwords;
import cn.iocoder.yudao.module.setting.tools.JsonToXml;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhyd.oauth.log.Log;
import org.apache.http.HttpResponse;
@ -15,8 +16,10 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.*;
@ -56,8 +59,6 @@ public class PasswordsServiceImpl implements PasswordsService {
wrapper.orderByDesc("create_time").last("LIMIT 1");
PasswordsDO passwordsDO = passwordsMapper.selectOne(wrapper);
String passwordValueMD5 = passwordsDO.getPasswordValue();
// 获取PrintWriter对象
PrintWriter out = httpResponse.getWriter();
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("application/json");
if (passwordValueMD5 == null) {
@ -65,25 +66,34 @@ public class PasswordsServiceImpl implements PasswordsService {
json2.put("code", "500");
json2.put("msg", "password is null");
httpResponse.setStatus(500);
PrintWriter out = httpResponse.getWriter(); // 获取PrintWriter对象
out.println(json2.toString());
out.flush();
return null; // 返回XML字符串
} else {
boolean passwordValid = this.isPasswordValid(inputPassword, passwordValueMD5);
if (passwordValid) {//将json字符串转化为JSONObject对象
JSONObject jsonObject = new JSONObject(json);
//将JSONObject对象转化为xml字符串
String xml = XML.toString(jsonObject);
httpResponse.setStatus(200); // 设置响应状态码为200
httpResponse.setHeader("Content-Type", ContentType.APPLICATION_XML.toString());
out.println(xml);
return xml; // 返回XML字符串
if (passwordValid) {
//1.设置文件下载的response响应格式
String fileType = "xml"; //文件类型
httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("元数据." + fileType, "UTF-8"));
httpResponse.setContentType("multipart/form-data");
httpResponse.setCharacterEncoding("UTF-8");
String jsonToXml = JsonToXml.jsonToXml(json);
//3.将内容转为byte[]格式
byte[] data2 = jsonToXml.getBytes("UTF-8");
//4.将内容写入响应流
OutputStream out2 = httpResponse.getOutputStream();
out2.write(data2);
out2.flush();
out2.close();
return jsonToXml;
} else {
JSONObject json2 = new JSONObject();
json2.put("code", "500");
json2.put("msg", "passwordError!");
httpResponse.setStatus(500);
PrintWriter out = httpResponse.getWriter(); // 获取PrintWriter对象
out.println(json2.toString());
out.flush();
return null;
@ -91,6 +101,8 @@ public class PasswordsServiceImpl implements PasswordsService {
}
}
@Override
public Long createPasswords(PasswordsCreateReqVO createReqVO) {
// 插入

@ -0,0 +1,92 @@
package cn.iocoder.yudao.module.setting.tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author ysd
* @date 20210803 9:33
*/
public class JsonToXml {
/**
* Json to xml string.
*
* @param json the json
* @return the string
*/
public static String jsonToXml(String json){
try {
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
JSONObject jObj = JSON.parseObject(json);
jsonToXmlstr(jObj,buffer);
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* Json to xmlstr string.
*
* @param jObj the j obj
* @param buffer the buffer
* @return the string
*/
public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer) {
Set<Map.Entry<String, Object>> se = jObj.entrySet();
for (Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); ) {
Map.Entry<String, Object> en = it.next();
if (en.getValue() instanceof JSONObject) {
buffer.append("<" + en.getKey() + ">");
JSONObject jo = (JSONObject) en.getValue();
jsonToXmlstr(jo, buffer);
buffer.append("</" + en.getKey() + ">");
} else if (en.getValue() instanceof JSONArray) {
JSONArray jarray = (JSONArray) en.getValue();
for (int i = 0; i < jarray.size(); i++) {
buffer.append("<" + en.getKey() + ">");
JSONObject jsonobject = jarray.getJSONObject(i);
jsonToXmlstr(jsonobject, buffer);
buffer.append("</" + en.getKey() + ">");
}
} else if (en.getValue().getClass().getName().equals("java.lang.String")) {
buffer.append("<" + en.getKey() + ">" + encodeXmlString((String) en.getValue()));
buffer.append("</" + en.getKey() + ">");
}
}
return buffer.toString();
}
public static String encodeXmlString(String input) {
StringBuilder out = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c <= 0xD7FF || c >= 0xE000) {
out.append(c);
} else {
out.append("&#x").append(Integer.toHexString(c)).append(";");
}
}
return out.toString();
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
String xmlstr = jsonToXml("{\"class\": {\"student\": [{\"age\": \"18\",\"gender\": \"男\",\"name\": \"张三\",\"qk\": [{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"},{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"}]},{\"age\": \"17\",\"gender\": \"男\",\"name\": \"李四\",\"qk\": {\"q1\": \"005\",\"q2\": \"006\",\"q3\": \"007\"}},{\"age\": \"19\",\"gender\": \"女\",\"name\": \"王五\",\"qk\": {\"q1\": \"008\",\"q2\": \"009\",\"q3\": \"010\"}}]}}");
System.out.println(xmlstr);
}
}
Loading…
Cancel
Save