简体   繁体   中英

Uploading SmbFile using Apache Commons HttpClient

I have been trying to upload SmbFile using Apache Commons HttpClient, Existing system uses org.​apache.​commons.​httpclient.​methods.​multipart.FilePart with native java.io.File, I need to modify it to support smbfile but both FilePart and FilePartSource seems to accept java.io.File only. Is there a way i can cast jcifs.smb.SmbFile to java.io.File or is there any other workaround to achieve this?

Update: I am making this edit to give full picture of my requirement, so that it might be useful for someone trying to answer.

I want to upload a file that resides in shared file server to some HTTP URL. As said earlier, existing system uses Apache Commons HttpClient to achieve this, but unfortunately it requires Native IO file. I am unable to create Native IO file object, since the file is inside password protected file server. I made a workaround to connect the network drive using NET command in windows there by creating native file object. however this method is not consistent because sometimes i am getting System error 1312 has occurred. A specified logon session does not exist. It may already have been terminated.

If the information provided is not sufficient, please post a comment.

Finally, i have found the workaround to make things work, i am sharing this for someone coming here looking for the answer.

As of now, there is no direct method available (correct me if i am wrong). Only way i know to make this work is to create a SmbFile using.

SmbFile smbFile = new SmbFile(filePath, ntlmAuth);

then, create inputstream out of smbFile by using,

InputStream is = new SmbFileInputStream(smbFile);

Convert inputstream to byte array using Apache Commons IOUtils (other methods can also be used, my choice was Apache).

byte[] fileBytes = IOUtils.toByteArray(is);

Create ByteArrayPartSource object,

PartSource ps = new ByteArrayPartSource(fileName, fileBytes);

Then FilePart can be created using the partsource object.

new FilePart("uploadedFile", ps);

Voila, now you can upload your file using Apache Commons HttpClient.

Anyone having better approach than this, please feel free to post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM