[jcifs] Problem (and fix) accessing file contents using smb: url

Thierry Lach thierry.lach at gmail.com
Tue Nov 14 18:22:25 GMT 2006

I'm trying to access a files on a unix system through a Samba share from
java inside of a web application.  I want to keep the password configuration
separate from the url for security purposes.

I'm using jcifs.Config to set all of the properties, and can access the smb:
protocol properly.  But when I try to access the file contents (as in the
following example) I get an error indicating that the content type has not
been set..

    URL url = new URL("smb://hostname/sharename/filename.xml");
    URLConnection conn = url.openConnection();
    Object contents = conn.getContent();
    if (contents instanceof InputStream) {
        BufferedReader reader = new BufferedReader(new
InputStreamReader((InputStream) contents));
        String line;
        while ( null != (line = reader.readLine())) {

I added the following code to SmbFile and now it works...

     * This URLConnection method returns a generic content type.
     * @see java.net.URLConnection#getContentType()
    public String getContentType() {
        String contentType = super.getContentType();
        if (contentType == null) {
            // Call it binary since we don't know any better
            contentType = "application/octet-stream";
        return contentType;

This can probably look at file extensions to better select a content type,
but this is a minimalist fix.
-------------- next part --------------
HTML attachment scrubbed and removed

More information about the jcifs mailing list