我有一个程序可以解码 pdf 并通过套接字发送它。我查看了很多关于堆栈溢出的代码,但无法找到解决问题的方法。android 客户端读取文本,将其转换为字节码并将文件写入应该由 pdfViewer 加载的缓存。pdf 查看器上的任何测试以及与客户端通信的消息都有效,但是当文件通过套接字发送时,pdf 已损坏。该文件实际上已创建,如果没有向其写入字节,将返回 pdf 为空的错误。我排除了错误捕获和其他信息,因为其余代码无关:
服务器:
File f = new File(PATH_TO_PDF);
FileInputStream is = new FileInputStream(f);
byte[] pdf = new byte[(int)(f.length())];
int a;
int count = 0;
while ((a=is.read())!= -1){
pdf[count] = (byte)a;
count++;
}
is.close();
String result = "";
for (int i = 0; i < pdf.length; i++) {
returnMessage.append(pdf[i]);
}
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage + "\n");
System.out.println("Message sent to the client is "+ returnMessage);
bw.flush();
//rest is closing socket stuff
客户:
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
s.close();
byte[] bytes = message.getBytes();
File someFile = new File(getCacheDir() + "/file.pdf");
FileOutputStream fos = new FileOutputStream(someFile);
fos.write(bytes);
fos.flush();
fos.close();
任何帮助是极大的赞赏!
开满天机
相关分类