几天后,我在Java中摆弄一个使用RESTful webservice发送/接收文件的项目。我使用尽可能少的额外库来执行此操作,因为这将是现有软件系统的扩展。为了让我的项目在这个软件系统中运行,我需要遵循他们的API。我需要提供我的'扩展'作为OSGI包。
因为我的项目的结果需要运行snappy并且可能在移动设备上运行,我还想在文件上使用GZip压缩。当我阅读有关通过REST发送文件的内容时,最好对Base64进行编码,然后使用它发送文件名。
这使我看到以下客户端代码示例:(不是实际的“扩展名”)
IWorkOrderService workOrderService = connection.getImplementation(IWorkOrderService.class);
File file = Paths.get("C:/Temp/", "filename.txt").toFile();
PlnJsonResponse response = workOrderService.createDocument("pln", "dBM", file.getName(), base64EncodeAndGZipFile(file));
System.out.println("WorkOrder Service createDocument: success? " + response.isSuccess() + ", Message: '" + response.getMessage() + "'");
private static String base64EncodeAndGZipFile(File file) {
try (FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Base64OutputStream base64OutputStream = new Base64OutputStream(byteArrayOutputStream);
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(base64OutputStream)) {
int len;
byte[] buffer = new byte[1024];
while((len=fis.read(buffer)) != -1){
gzipOutputStream.write(buffer, 0, len);
}
gzipOutputStream.finish();
String byteArrayAsString = byteArrayOutputStream.toString("UTF-8");
System.out.println("byteArrayAsString: '" + byteArrayAsString.length() + "' bytes");
System.out.println(byteArrayAsString);
return byteArrayAsString;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
从日志输出中,这部分似乎工作正常。 (前3行)
byteArrayAsString: '102' bytes
H4sIAAAAAAAAAAvJyCxWAKJEhZLU4hKFtMycVIW0/CKFINfgkPgUJ1/HxLy81NzUomJeLl1iAS+X
T2ZeqoIhlDaC0sYAwQ1Gr2wA
WorkOrder Service createDocument: success? false, Message: 'CreateDocument: uncompressedByteArray: 'null''
当我尝试在我的服务中读取,解码和解压缩此问题时,会出现问题。 (软件系统上的“扩展名”)打印在上面日志的最后一行。
接收电话的代码如下:
private static final byte NEWLINE = (byte) '\n';
private static final byte CARRIAGE_RETURN = (byte) '\r';
private static final byte[] LINE_SEPARATORS = new byte[] {CARRIAGE_RETURN, NEWLINE};
public PlnJsonResponse createDocument(String plnOrderNumber, String dBMOrderNumber, String fileName, String byteArrayAsString) {
System.out.println(">> createDocument <<");
System.out.println(">> byteArrayAsString: '" + byteArrayAsString.length() + "' bytes");
System.out.println(byteArrayAsString);
System.out.println("<< isBase64? " + Base64.isBase64(byteArrayAsString));
final byte[] compressedByteArray = byteArrayAsString.getBytes(Charset.forName("UTF-8"));
byte[] uncompressedByteArray = null;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressedByteArray);
Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream, false, 76, LINE_SEPARATORS);
GZIPInputStream gzipInputStream = new GZIPInputStream(base64InputStream, 1024);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int len;
while((len = gzipInputStream.read(buffer, 0, buffer.length)) != -1){
byteArrayOutputStream.write(buffer, 0, len);
}
uncompressedByteArray = byteArrayOutputStream.toByteArray();
String uncompressedByteArrayAsString = byteArrayOutputStream.toString("UTF-8");
System.out.println(">> uncompressedByteArrayAsString: '" + uncompressedByteArrayAsString.length() + "'");
System.out.println(uncompressedByteArrayAsString);
System.out.println("<<");
} catch (IOException e) {
e.printStackTrace();
}
return new PlnJsonResponse.Builder().withMessage("CreateDocument: uncompressedByteArray: '" + uncompressedByteArray + "'").build();
}
我环顾网络和stackoverflow,但似乎善良。我从帖子中得到了一些想法: How to convert Java String into byte[]? 和 Stream decoding of Base64 data 没有成功。必须有一些我忽略的东西。
从服务器登录是:
INFO | jvm 1 | 2015/05/04 20:35:11 | >> createDocument <<
INFO | jvm 1 | 2015/05/04 20:35:11 | << isBase64? true
INFO | jvm 1 | 2015/05/04 20:35:11 | java.io.EOFException
INFO | jvm 1 | 2015/05/04 20:35:11 | at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:264)
INFO | jvm 1 | 2015/05/04 20:35:11 | at java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:254)
INFO | jvm 1 | 2015/05/04 20:35:11 | at java.util.zip.GZIPInputStream.readUInt(GZIPInputStream.java:247)
INFO | jvm 1 | 2015/05/04 20:35:11 | at java.util.zip.GZIPInputStream.readTrailer(GZIPInputStream.java:218)
INFO | jvm 1 | 2015/05/04 20:35:11 | at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:118)
INFO | jvm 1 | 2015/05/04 20:35:11 | at ***.impl.WorkOrderService.createDocument(WorkOrderService.java:61)
INFO | jvm 1 | 2015/05/04 20:35:11 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
当我评论出GZip压缩时,这很好用。 任何帮助或提示将不胜感激。
亲切的问候