JSch get() fails with NullPointerException

妖精的绣舞 提交于 2019-12-09 23:55:57

问题


I've got a Jax-RS server which is supposed to keep a list of files accessible via ssh that I can then download or stream via HTTP.

I've been trying to read the files with JSch's SFTP channel, but I keep receiving a NullPointerException.

Here's the MessageBodyWriter I wrote:

@Provider
@Produces("video/*")
public class MediaBodyWriter implements MessageBodyWriter<MediaFile> {

    @Override
    public long getSize(MediaFile mFile, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
        return mFile.getFileSize();
    }

    @Override
    public boolean isWriteable(Class<?> type, Type arg1, Annotation[] arg2, MediaType arg3) {
        return type.equals(MediaFile.class);
    }

    @Override
    public void writeTo(MediaFile mFile,
                    Class<?> type,
                    Type genericType,
                    Annotation[] annotations,
                    MediaType mediaType,
                    MultivaluedMap<String, Object> httpHeaders,
                    OutputStream entityStream) throws IOException, WebApplicationException {
        mFile.streamFile(entityStream); //line 41
    }

}

Here's MediaFile class minus the getters and setters:

@Entity
@XmlRootElement
public class MediaFile {
    @Id
    @GeneratedValue
    Long id;

    @JsonIgnore
    @ManyToOne(cascade=CascadeType.ALL)
    @PrimaryKeyJoinColumn
    MediaRepo repo;

    String filePath;
    String fileName;

    transient JSch jsch;
    transient Session session;
    transient ChannelSftp sftp;

    public MediaFile(){}

    public void prepForDownload(){
        if(sftp != null)
            return;
        try{
            jsch = new JSch();
            session = jsch.getSession(repo.getUsername(), repo.getHost(), repo.getPort());
            session.setPassword(repo.getPassword());
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            sftp = (ChannelSftp)session.openChannel("sftp");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public long getFileSize(){
        prepForDownload();
        try {
            return sftp.lstat(getCompletePath()).getSize();
        } catch (SftpException e) {
            e.printStackTrace();
        }
        return 0;
    }
    private String getCompletePath(){
        return repo.getBasePath()+filePath+fileName;
    }
    public void streamFile(OutputStream output){
        prepForDownload();
        try {
            sftp.get(getCompletePath(), output);
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
}

And here's the error I'm receiving:

13:50:05,260 ERROR [stderr] (default task-5) 4: 
13:50:05,260 ERROR [stderr] (default task-5)    at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1011)
13:50:05,260 ERROR [stderr] (default task-5)    at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:984)
13:50:05,261 ERROR [stderr] (default task-5)    at model.MediaFile.streamFile(MediaFile.java:75)
13:50:05,261 ERROR [stderr] (default task-5)    at MediaBodyWriter.writeTo(MediaBodyWriter.java:41)
13:50:05,261 ERROR [stderr] (default task-5)    at MediaBodyWriter.writeTo(MediaBodyWriter.java:1)
13:50:05,261 ERROR [stderr] (default task-5)    at MediaBodyWriter$Proxy$_$$_WeldClientProxy.writeTo(Unknown Source)
13:50:05,261 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:131)
13:50:05,261 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:60)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:120)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.security.doseta.DigitalSigningInterceptor.aroundWriteTo(DigitalSigningInterceptor.java:145)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:98)
13:50:05,262 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:466)
13:50:05,263 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:415)
13:50:05,263 ERROR [stderr] (default task-5)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202)
13:50:05,263 ERROR [stderr] (default task-5)    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
13:50:05,263 ERROR [stderr] (default task-5)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
13:50:05,263 ERROR [stderr] (default task-5)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
13:50:05,263 ERROR [stderr] (default task-5)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
13:50:05,263 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
13:50:05,263 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
13:50:05,264 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
13:50:05,264 ERROR [stderr] (default task-5)    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
13:50:05,264 ERROR [stderr] (default task-5)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:50:05,264 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
13:50:05,264 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
13:50:05,264 ERROR [stderr] (default task-5)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:50:05,265 ERROR [stderr] (default task-5)    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
13:50:05,265 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
13:50:05,265 ERROR [stderr] (default task-5)    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
13:50:05,265 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
13:50:05,265 ERROR [stderr] (default task-5)    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
13:50:05,266 ERROR [stderr] (default task-5)    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
13:50:05,266 ERROR [stderr] (default task-5)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:50:05,266 ERROR [stderr] (default task-5)    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
13:50:05,266 ERROR [stderr] (default task-5)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:50:05,266 ERROR [stderr] (default task-5)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:50:05,266 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
13:50:05,267 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
13:50:05,267 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
13:50:05,267 ERROR [stderr] (default task-5)    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
13:50:05,267 ERROR [stderr] (default task-5)    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
13:50:05,267 ERROR [stderr] (default task-5)    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
13:50:05,267 ERROR [stderr] (default task-5)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
13:50:05,268 ERROR [stderr] (default task-5)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
13:50:05,268 ERROR [stderr] (default task-5)    at java.lang.Thread.run(Thread.java:745)
13:50:05,268 ERROR [stderr] (default task-5) Caused by: java.lang.NullPointerException
13:50:05,268 ERROR [stderr] (default task-5)    at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:994)
13:50:05,269 ERROR [stderr] (default task-5)    ... 47 more

回答1:


You have to call the .connect() on the ChannelSftp, before you can use it, as all JSch SFTP examples show.

For example, see the official JSch SFTP example:

Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;


来源:https://stackoverflow.com/questions/37799006/jsch-get-fails-with-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!