使用jsch通过rest api完全写入文件

时间:2017-05-23 05:52:21

标签: java rest file jsch

我正在尝试使用jsch通过rest api下载文件。我试图在unix框中读取文件并将其写入文件并下载文件。当我使用下面的代码时,文件的内容没有完整写入。我正在尝试读取一个大文件,但文件没有被完全读取。

 @GET
    @Path("/pdf")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
     public Response readFile() {


        StreamingOutput  fileStream =  new StreamingOutput() 
                    {
                        @Override
                        public void write(java.io.OutputStream output) throws IOException, WebApplicationException 
                        { Session session=null;
                         JSch jsch=new JSch();
                            try {

                        session = jsch.getSession("username", "ipAddress", 22);
                            session.setPassword("password");;
                            session.setConfig("StrictHostKeyChecking", "no");
                        session.connect();
                            final ChannelExec channel = (ChannelExec) session.openChannel("exec");
                            channel.setCommand("cd /path/; cat filename");
                            channel.setInputStream(null);
                            channel.setErrStream(System.err);
                            final InputStream input = channel.getInputStream();
                            InputStream error = channel.getErrStream();
                            channel.connect();
                            Thread.sleep(30000);
                            byte[] tmp = new byte[100000];
                            while (true) {
                                if (input.available() > 0) {
                                    int i = input.read(tmp, 0, 100000);
                                        if (i < 0)
                                        break;
                                    final StringBuilder temp = new StringBuilder();
                                     temp.append((new String(tmp, 0, i)));                                  

                                    File file=new File("D://mylog.log");
                                    FileOutputStream oS = new FileOutputStream(new File("D://mylog.log"));
                                    oS.write(temp.toString().getBytes());
                                    java.nio.file.Path path = Paths.get("D://mylog.log");                               
                                byte[] data = Files.readAllBytes(path);
                                output.write(data);
                                output.flush();
                                break;
                                }

                            }}
                            catch (Exception e) {
                                e.printStackTrace();

                            } finally {

                            }
                        }
                    };

                    return Response
                            .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
                            .header("content-disposition","attachment; filename = myfile.log")
                            .build();


    }

0 个答案:

没有答案