From 1a3ad048aa43f0b6167764d3ce6ec8736bf58272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E8=BF=9E=E4=BA=91?= <562050688@qq.com> Date: Thu, 13 Jul 2023 09:30:46 +0000 Subject: [PATCH 1/2] =?UTF-8?q?add=20java/Utf8.java.=20=E5=9D=91=E7=82=B9:?= =?UTF-8?q?=201.=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6=E5=85=A8=E9=83=A8?= =?UTF-8?q?=E5=86=85=E5=AE=B9=202.=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8E=E5=87=BA=E7=8E=B0=E5=BC=82=E5=B8=B8,=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E4=B8=80=E4=B8=8B=E5=B0=B1=E5=AE=8C=E4=BA=8B=203.try?= =?UTF-8?q?=E5=B5=8C=E5=A5=97=204.isUtf8=20=E5=88=A4=E6=96=AD=E7=9A=84?= =?UTF-8?q?=E5=A4=AA=E6=9A=B4=E5=8A=9B=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 苏连云 <562050688@qq.com> --- java/Utf8.java | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 java/Utf8.java diff --git a/java/Utf8.java b/java/Utf8.java new file mode 100644 index 0000000..be63ae6 --- /dev/null +++ b/java/Utf8.java @@ -0,0 +1,80 @@ +/** + * 如果文件是utf8编码返回true,反之false + * + * @param file + * @return + */ + private Boolean isUtf8(File file) { + boolean isUtf8 = true; + byte[] buffer = readByteArrayData(file); + int end = buffer.length; + for (int i = 0; i < end; i++) { + byte temp = buffer[i]; + if ((temp & 0x80) == 0) { + continue; + } else if ((temp & 0xC0) == 0xC0 && (temp & 0x20) == 0) { + if (i + 1 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0) { + i = i + 1; + continue; + } + } else if ((temp & 0xE0) == 0xE0 && (temp & 0x10) == 0) {// 1110xxxx 10xxxxxx 10xxxxxx + if (i + 2 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0 + && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0) { + i = i + 2; + continue; + } + } else if ((temp & 0xF0) == 0xF0 && (temp & 0x08) == 0) {// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + if (i + 3 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0 + && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0 + && (buffer[i + 3] & 0x80) == 0x80 && (buffer[i + 3] & 0x40) == 0) { + i = i + 3; + continue; + } + } + isUtf8 = false; + break; + } + return isUtf8; + } + + /** + * 从文件中直接读取字节 + * + * @param file + * @return + */ + private byte[] readByteArrayData(File file) { + byte[] rebyte = null; + BufferedInputStream bis; + ByteArrayOutputStream output; + try { + bis = new BufferedInputStream(new FileInputStream(file)); + output = new ByteArrayOutputStream(); + byte[] byt = new byte[1024 * 4]; + int len; + try { + while ((len = bis.read(byt)) != -1) { + if (len < 1024 * 4) { + output.write(byt, 0, len); + } else { + output.write(byt); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + rebyte = output.toByteArray(); + if (bis != null) { + bis.close(); + } + if (output != null) { + output.close(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return rebyte; + } \ No newline at end of file -- Gitee From bb52e366d73934c8ec54d27657aa5b7b97622f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E8=BF=9E=E4=BA=91?= <562050688@qq.com> Date: Thu, 13 Jul 2023 09:47:41 +0000 Subject: [PATCH 2/2] =?UTF-8?q?add=20java/GetFileType.java.=20=E5=9D=91?= =?UTF-8?q?=E7=82=B9:=201.=E9=80=9A=E8=BF=87=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=202.=E6=9E=81=E5=85=B6=E7=82=B8=E8=A3=82=E7=9A=84equals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 苏连云 <562050688@qq.com> --- java/GetFileType.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 java/GetFileType.java diff --git a/java/GetFileType.java b/java/GetFileType.java new file mode 100644 index 0000000..8ffdad3 --- /dev/null +++ b/java/GetFileType.java @@ -0,0 +1,24 @@ + public static String GetFileType(String originalFilename) { + //获取最后一个.的位置 + int lastIndexOf = originalFilename.lastIndexOf("."); + if (lastIndexOf < 0) { + return ""; + } + //获取文件的后缀名 .jpg + String suffix = originalFilename.substring(lastIndexOf); + if (suffix.equals(".gz")) { + return ".gz"; + } else if (suffix.equals(".zip")) { + return ".zip"; + } else if (suffix.equals(".csv")) { + return ".csv"; + } else if (suffix.equals(".xml")) { + return ".xml"; + } else if (suffix.equals(".xls")) { + return ".xls"; + } else if (suffix.equals(".xlsx")) { + return ".xlsx"; + } else { + return ""; + } + } \ No newline at end of file -- Gitee