使用Jackcess和JCIFS来操作SMB共享上的Access数据库

时间:2016-02-01 00:20:57

标签: java macos smb jackcess

我需要使用Jackcess在Java中使用MS Access文件。该文件位于SMB共享上,因此我假设我必须使用JCIFS。

我试过这个

    String testdirectory = "smb://" + "file location"; 

    SmbFile testsmbdir = null;

    try{
            testsmbdir = new SmbFile(testdirectory,auth);
    }catch(Exception e){
                e.printStackTrace();
    }


    SmbFileInputStream smbFilestream = new SmbFileInputStream(testsmbdir);
    db = DatabaseBuilder.open(testsmbdir);

但是,它说SMBFile无法转换为

的文件
db = DatabaseBuilder.open(testsmbdir)" 

线。此外,如果我尝试使用“smbFilestream”而不是它说它无法将SmbFileInputStream转换为File。

我是否必须将文件复制到本地计算机或完全不同的东西?如果我该怎么办?

(顺便说一下,我是一名Windows用户。我只是将我的应用程序转换为Mac,如果我的术语关闭,请对不起。)

1 个答案:

答案 0 :(得分:4)

在回答Jackcess论坛here上的帖子时,詹姆斯建议

  

实现与SmbRandomAccessFile一起使用的FileChannel版本应该相对简单

我刚刚在Eclipse中一个名为> 02-Feb-2016 11:15:28.949 SEVERE [http-nio-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [DispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.webflow.execution.FlowExecutionException: Exception thrown in state 'individualAdd' of flow 'application'] with root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'client' available as request attribute at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116) at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422) at org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:194) at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80) at org.apache.jsp.WEB_002dINF.flows.application.individualAdd_jsp._jspx_meth_form_005fselect_005f0(individualAdd_jsp.java:1660) at org.apache.jsp.WEB_002dINF.flows.application.individualAdd_jsp._jspx_meth_form_005fform_005f0(individualAdd_jsp.java:1482) at org.apache.jsp.WEB_002dINF.flows.application.individualAdd_jsp._jspService(individualAdd_jsp.java:378) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) at org.springframework.webflow.mvc.servlet.ServletMvcView.doRender(ServletMvcView.java:55) at org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:196) at org.springframework.webflow.engine.ViewState.render(ViewState.java:293) at org.springframework.webflow.engine.ViewState.refresh(ViewState.java:242) at org.springframework.webflow.engine.ViewState.resume(ViewState.java:220) at org.springframework.webflow.engine.Flow.resume(Flow.java:537) at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:259) at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169) at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:228) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 的Maven项目中尝试过它,而且我无需编写太多代码就可以使用它。我创建的类名为smb4jackcess

SmbFileChannel

我使用的主要课程是

// FileChannel using jcifs.smb.SmbRandomAccessFile

package smb4jackcess;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbRandomAccessFile;

public class SmbFileChannel extends FileChannel {
    private final SmbRandomAccessFile _file;
    private long _length;

    public SmbFileChannel(String smbURL) throws SmbException, MalformedURLException, UnknownHostException {
        _file = new SmbRandomAccessFile(smbURL, "rw", SmbFile.FILE_NO_SHARE);
        _length = _file.length();
    }

    @Override
    public void force(boolean metaData) throws SmbException, MalformedURLException, UnknownHostException {
        // do nothing
    }

    @Override
    public FileLock lock(long position, long size, boolean shared) {
        throw new UnsupportedOperationException();
    }

    @Override
    public MappedByteBuffer map(MapMode mode, long position, long size) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long position() throws SmbException {
        return _file.getFilePointer();
    }

    @Override
    public FileChannel position(long newPosition) throws SmbException {
        _file.seek(newPosition);
        return this;
    }

    @Override
    public int read(ByteBuffer dst) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int read(ByteBuffer dst, long position) throws SmbException {
        byte[] b = new byte[dst.remaining()];
        _file.seek(position);
        int bytesRead =_file.read(b);
        dst.put(b);
        return bytesRead;
    }

    @Override
    public long read(ByteBuffer[] dsts, int offset, int length) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long size() throws SmbException {
        return _length;
    }

    @Override
    public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate((int)count);
        int bytesWritten = src.read(bb);
        bb.rewind();
        bb.limit(bytesWritten);
        this.write(bb, position);
        return bytesWritten;
    }

    @Override
    public long transferTo(long position, long count, WritableByteChannel target) {
        throw new UnsupportedOperationException();
    }

    @Override
    public FileChannel truncate(long newSize) throws SmbException {
        if (newSize < 0L) {
            throw new IllegalArgumentException("negative size");
        }
        _file.setLength(newSize);
        _length = newSize;
        return this;
    }

    @Override
    public FileLock tryLock(long position, long size, boolean shared) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int write(ByteBuffer src) throws SmbException {
        throw new UnsupportedOperationException();
    }

    @Override
    public int write(ByteBuffer src, long position) throws SmbException {
        byte[] b = new byte[src.remaining()];
        src.get(b);
        _file.seek(position);
        _file.write(b);

        long endPos = position + b.length;
        if(endPos > _length) {
            _length = endPos;
        }

        return b.length;
    }

    @Override
    public long write(ByteBuffer[] srcs, int offset, int length) {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void implCloseChannel() throws SmbException {
        _file.close();
    }

}

更新2016-02-04:代码改进。非常感谢James在Dell Boomi的帮助下!