Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.net.ProtocolException: cannot write to a URLConnection

910067Jan 10 2012 — edited Jan 16 2012
I want to upload a file to a computer in the network. There is proxy so i had to set proxy settings but ı've got this exception:
java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
I couldn't solve this exception.

Here is my code :
StringBuffer filePath = new StringBuffer("file:\\\\10.253.8.77\\Folder");
Properties sysProperties = System.getProperties();

sysProperties.put("http.proxyHost", "proxy.XXX.intra");
sysProperties.put("http.proxyPort", "80");
sysProperties.put("http.proxySet", "true");

URL url = new URL( filePath.toString() );
URLConnection urlc = url.openConnection();
urlc.setDoInput (true);
urlc.setDoOutput (true);
urlc.setUseCaches(false);

urlc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
urlc.setAllowUserInteraction(true);

DataOutputStream dataOutputStream = (DataOutputStream)urlc.getOutputStream();

String content = "USERNAME=" + URLEncoder.encode ("XXXX") + "&PASSWORD=" + URLEncoder.encode ("987654321");

dataOutputStream.writeBytes (content);

//dataOutputStream.write(file.getFileData());
dataOutputStream.flush();
dataOutputStream.close();
This post has been answered by DrClap on Jan 11 2012
Jump to Answer

Comments

darrylburke
Moderator advice: Don't double post and don't use the browser's back button to edit your posts, as that creates multiple posts. The other thread you started 6 minutes before this one has been removed.

Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

Then edit your post and format the code correctly.
gimbal2
I'm more amazed that you do not get a ClassCastException. getOutputStream() returns an OutputStream, not necessarily a DataOutputStream.

Are you sure you are recompiling/deploying this code before trying to run it again?
910067
I don't get classCastException because DataOutputStream extends from OutputStream.
Tolls
907064 wrote:
I don't get classCastException because DataOutputStream extends from OutputStream.
Yes it does, but the OutputStream returned by getOutputStream may be anything else that extends OutputStream.
You can't just cast anything to anything else based on it having the same parent class, otherwise you could cast everything just because it was passed into a method as an Object.
DrClap
What's that business about proxies for? Configuring an HTTP proxy won't have any effect on a file:// URL.

And what's that business about sending an HTTP request to a file:// URL? That doesn't make any sense to me either.

Why don't you just use a File object? Or if you have authentication problems, why not use jCIFS to connect to the file?
EJP
What's that business about proxies for? Configuring an HTTP proxy won't have any effect on a file:// URL.
And configuring http.proxySet won't have any effect on anything anywhere. It is an urban myth. It doesn't exist.

@OP:

1. URLs cannot contain backslashes.
2. The file: protocol handler in Java does not support output.
910067
Thanks a lot for your replies.

I have tried to configure proxy settings , because I had java.io.FileNotFoundException: \\10.253.8.32\Denetleme (Acess is denied.) when I ran the following code:

StringBuffer filePath = new StringBuffer("\\\\10.253.8.32\\Denetleme");

outputStream = new FileOutputStream(new File(filePath.toString()));

outputStream.write(file.getFileData());
outputStream.flush();
outputStream.close();


I'm trying to upload a file to a different server. I'm getting a file from the user and i can download it to my server. But i can not put the file to another location like "\\10.253.8.32\Denetleme" folder.
gimbal2
\\\\10.253.8.32\\Denetleme
I may be sadly mistaken, but it seems like you are trying to open an outputstream to a directory.
DrClap
Answer
907064 wrote:
I had java.io.FileNotFoundException: \\10.253.8.32\Denetleme (Acess is denied.)
Then jCIFS is what you should try next. It allows you to specify the user ID and password to be used to access that share. (That is a share you're trying to upload to, right?)
Marked as Answer by 910067 · Sep 27 2020
910067
My problem is solved by using jcifs library. Thanks for your good advice and also thans for all replies.
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 13 2012
Added on Jan 10 2012
10 comments
5,710 views