{"id":117,"date":"2009-09-10T18:14:31","date_gmt":"2009-09-10T16:14:31","guid":{"rendered":"http:\/\/demeringo.ovh.org\/blog\/?p=117"},"modified":"2011-01-03T10:14:30","modified_gmt":"2011-01-03T09:14:30","slug":"write-and-read-to-a-samba-shared-directory-using-java-jcifs","status":"publish","type":"post","link":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/?p=117","title":{"rendered":"Write and read to a samba shared directory using java (JCIFS)"},"content":{"rendered":"<p>Challenge of the day: publish a file to a microsoft shared directory and then re-read it.<\/p>\n<p>Windows OSes rely on the SAMBA protocol to share files and directories. Thanks to the JCIFS project( <a href=\"http:\/\/jcifs.samba.org\/\">http:\/\/jcifs.samba.org\/<\/a>), an open source library, we can access theses kind of shares via java.<\/p>\n<p><em>Under linux, we could have performed it a the shell level using the <em>SMBclient<\/em> command line utility.<\/em><\/p>\n<p>The example is pretty self explanatory, just remember to add<em> jcifs-1.2.25.jar<\/em> to your classpath and to adapt the authentication parameters to your real login in the url.<\/p>\n<pre lang=\"java\">import jcifs.smb.*;\r\npublic class SMBClient {\r\n  public static void main(String[] args) {\r\n    try{\r\n      \/\/ jcifs.Config.setProperty( \"jcifs.netbios.wins\", \"192.168.1.220\" );\r\n      \/\/smb:\/\/[[[domain;]username[:password]@]server[:port]\/[[share\/[dir\/]file]][?[param=value[param2=value2[...]]]\r\n      String url=\"smb:\/\/domain;user_name:user_password@server_name\/directory\/test_file.txt\";\r\n      String content=\"hello !\";\r\n      SmbFileOutputStream out = new SmbFileOutputStream(url);\r\n      out.write(content.getBytes());\r\n\r\n      System.out.println(\"File written, now trying to re-read it:\");\r\n      SmbFileInputStream in = new SmbFileInputStream(url);\r\n      byte[] b = new byte[10000];\r\n      int n;\r\n      while(( n = in.read( b )) > 0 ) {\r\n        System.out.write( b, 0, n );\r\n      }\r\n    }catch(Exception e){\r\n  System.out.println(e);\r\n  }\r\n }\r\n}<\/pre>\n<p>Thanks to Cyril for the challenge \ud83d\ude42 and to the forums of <a href=\"http:\/\/www.developpez.net\/forums\/d258549\/java\/general-java\/apis\/io\/envoyer-fichier-vers-dossier-partage-windows\/\">developpez.net<\/a> (in french) for an example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Challenge of the day: publish a file to a microsoft shared directory and then re-read it. Windows OSes rely on the SAMBA protocol to share files and directories. Thanks to the JCIFS project( http:\/\/jcifs.samba.org\/), an open source library, we can access theses kind of shares via java. Under linux, we could have performed it a &hellip; <a href=\"https:\/\/le-moulin-de-verre.com\/fieldnotes\/?p=117\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Write and read to a samba shared directory using java (JCIFS)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[9,13,15],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5WcEf-1T","_links":{"self":[{"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/posts\/117"}],"collection":[{"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=117"}],"version-history":[{"count":3,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/le-moulin-de-verre.com\/fieldnotes\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}