浏览代码

解决重新打印订单时的编码问题

xian 4 年之前
父节点
当前提交
a7438d89c4
共有 1 个文件被更改,包括 31 次插入13 次删除
  1. 31 13
      watero-rst-web/src/main/java/com/iamberry/rst/controllers/order/AwaitSendController.java

+ 31 - 13
watero-rst-web/src/main/java/com/iamberry/rst/controllers/order/AwaitSendController.java

@@ -32,6 +32,7 @@ import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -43,6 +44,7 @@ import java.io.*;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -380,7 +382,7 @@ public class AwaitSendController {
         }
         FileChannel foChannel = null;
         try {
-            byte[] jsonByte = text.getBytes("UTF-8");
+            byte[] jsonByte = text.getBytes(StandardCharsets.UTF_8);
             ByteBuffer buffer = ByteBuffer.allocate(jsonByte.length);
             buffer.put(jsonByte);
             foChannel = new FileOutputStream(file).getChannel();
@@ -575,30 +577,46 @@ public class AwaitSendController {
 
     // 重新打印发货
     @ResponseBody
-    @RequestMapping("/rePrintOrder")
+    @RequestMapping(value = "/rePrintOrder", produces= MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
     @RequiresPermissions("order:listAwaitSendOrder")
     public ResponseJson rePrintOrder(@RequestParam("orderId") String orderId,@RequestParam("salesId") String salesId,
                                      HttpServletRequest request) throws IOException {
         String filePath = request.getServletContext().getRealPath("/common/send_order");
         filePath = filePath + File.separator + orderId + ".txt";
-        // 读取文件内容
-        FileReader reader = new FileReader(filePath);
-        StringBuilder stringBuilder = new StringBuilder();
-        char[] chars = new char[1024];
-        int len = 0;
-        while((len = reader.read(chars)) != -1) {
-            stringBuilder.append(new String(chars, 0, len));
-        }
-        reader.close();
-
         //查询是否是子母单
         boolean isSub = false;
         List<SalesOrder> orders = salesOrderService.listSublistCount(Integer.valueOf(salesId));
         if(orders != null && orders.size() > 0){
             isSub = true;
         }
+        return ResponseJson.getSUCCESS().addResponseKeyValue("json", readNIO(filePath)).addResponseKeyValue("isSub",isSub);
+    }
 
-        return ResponseJson.getSUCCESS().addResponseKeyValue("json", stringBuilder.toString()).addResponseKeyValue("isSub",isSub);
+    private String readNIO(String pathname) {
+        FileInputStream fin = null;
+        StringBuilder temp = new StringBuilder();
+        try {
+            fin = new FileInputStream(new File(pathname));
+            FileChannel channel = fin.getChannel();
+            ByteBuffer bf = ByteBuffer.allocate(1024);
+            int length;
+            while ((length = channel.read(bf)) != -1) {
+                bf.clear();
+                temp.append(new String(bf.array(), 0, length, StandardCharsets.UTF_8));
+            }
+            channel.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (fin != null) {
+                try {
+                    fin.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return temp.toString();
     }
 
     /**