sun.misc.BASE64Decoder在java应用程序中显示错误

在某些java文件中有一个用途:

import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; 

当我把那个java文件放在Eclipse IDE中时。 它不会检测这些文件。 但是这些包(sun.misc.BASE64Decoder,sun.misc.BASE64Encoder)都在rt.jar文件中。 在我的项目库中,“rt.jar”可用。 但为什么它会显示错误(eclipse中的红线)?

不要在sun.misc包中使用类。 这些都被弃用并且很糟糕。 查看Apache commons编解码器的base64编码和解码。 为什么不使用sun.misc。*类

我为Base64编码和解码编写了以下代码:

 public static String base64Encode(String filename, boolean padding) { char[] base64Chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").toCharArray(); FileInputStream dataToEncode; StringBuilder encodedData = new StringBuilder(); byte[] dataBuffer = new byte[3]; int bytesRead; try { dataToEncode = new FileInputStream(filename); } catch (FileNotFoundException fnfe) { System.err.println("File not found!!"); return ""; } try { bytesRead = dataToEncode.read(dataBuffer); // cast to int, to avoid sign issues on the byte int buffer0 = dataBuffer[0] & 0xFF; int buffer1 = dataBuffer[1] & 0xFF; int buffer2 = dataBuffer[2] & 0xFF; if (bytesRead == -1) { System.err.println("Premature END OF FILE (nothing read)!!"); dataToEncode.close(); return ""; } while (bytesRead == 3) { // calculation of the base64 digits int b641 = buffer0 >>> 2; int b642 = ((buffer0 & 0x03) << 4) | (buffer1 >>> 4); int b643 = ((buffer1 & 0x0F) << 2) | (buffer2 >>> 6); int b644 = buffer2 & 0x3F; // generation of the 4 base64 chars, for the 3 bytes encodedData.append(base64Chars[b641]); encodedData.append(base64Chars[b642]); encodedData.append(base64Chars[b643]); encodedData.append(base64Chars[b644]); bytesRead = dataToEncode.read(dataBuffer); buffer0 = dataBuffer[0] & 0xFF; buffer1 = dataBuffer[1] & 0xFF; buffer2 = dataBuffer[2] & 0xFF; } if (bytesRead == 2) { encodedData.append(base64Chars[buffer0 >>> 2]); encodedData.append(base64Chars[((buffer0 & 0x03) << 4) | (buffer1 >>> 4)]); encodedData.append(base64Chars[((buffer1 & 0x0F) << 2)]); // exclude the last byte, that's just 0 if (padding) { // add the '=' character for padding, if the user wants encodedData.append('='); } } else if (bytesRead == 1) { encodedData.append(base64Chars[buffer0 >>> 2]); encodedData.append(base64Chars[((buffer0 & 0x03) << 4)]); if (padding) { encodedData.append("=="); } } dataToEncode.close(); return encodedData.toString(); } catch (IOException e) { System.out.println("Error reading file!!"); return ""; } } public static byte[] base64Decode(String encodedData) { String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // remove padding encodedData = encodedData.split("=")[0]; // create the byte buffer to unpack the bytes into byte[] result = new byte[(int) (encodedData.length() / 4) * 3 + encodedData.length() % 4 - 1]; int readingPosition = 0, writingPosition = 0, b641, b642, b643, b644; while (encodedData.length() - readingPosition > 3) { b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b644 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) ((b641 << 2) | (b642 >>> 4)); result[writingPosition++] = (byte) (((b642 & 0x0F) << 4) | (b643 >>> 2)); result[writingPosition++] = (byte) (((b643 & 0x03) << 6) | b644); } b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) ((b641 << 2) | (b642 >>> 4)); if (encodedData.length() % 4 == 3) { b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) (((b642 & 0x0F) << 4) | (b643 >>> 2)); } return result; }