Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a text file via URLConnection  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an applet, and I want it to be able to write to a file that resides on the SERVER side. In this code I can write to the OutputStream, but the text file itself is never written to. Any suggestions?
(Here is the code I am using)
public void btnAdd_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
try{
URL myUrl=new URL("http://localhost:8080/kprepog_format.txt");
URLConnection myUrlConn=myUrl.openConnection();
myUrlConn.setDoOutput(true);
PrintWriter out=new PrintWriter(myUrlConn.getOutputStream());
String first="1:1403:IP: :1: :";
String last=": :N:N:N: : :";
String temp="APPLES";
first=first+temp+last;
out.println();
out.println(first);
}
catch(Throwable e){
getlstCurrent().add(e.getMessage());
}
}
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Check the permissions on the file you are writing to. Failing that, make sure that the directory you are trying to store the file has write permissions for the proper users.
All web 'processes' run as a user. In the case of apache it may be defined as the user 'nobody' or in IIS (Internet Information Server/Personal Web Server by Microsoft) it is, by default, IUSR_MACHINE (where MACHINE is the name of your server). If the file you want to create does not already exist you will have to have write permissions to the directory/folder you want to create the file in. Otherwise you only need write permissions to the file itself.
My suggestion, if you can create the file before hand with maybe some kind of comment in it. Or, if you need to make a batch of files, create a seperate folder and give these 'users' full access to the sub folder only.
This will help reduce migraines and ulcers in your sys admins.
Also, as am aside note if you indent AND encapsulate your code in 'code' tags (see ubb code docs to the left) then it's much easier for us to read.

Thanks.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are missing something important here. You can't just write to a file from a remote machine, that would be incredibly insecure. You have to have some sort of server running to write to the file for you; then your applet has to talk the protocol that server supports over the socket connection. There are plenty of server protocols available (FTP, NFS, HTTP-PUT, SMB, WEBDAV etc. etc.) or you can write your own. In this example, though, you don't seem to have any server code at all.
The usual solution for small amounts of application-specific data, is to write a simple servlet and use HTTP from the applet to talk to it. The applet wraps the data up as a HTTP "POST" request, and the servlet takes the data and writes it to the local disk.
Almost as easy is to write a very simple server application which writes the raw socket data to a file every time it accepts a connection on a particular port.
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
Sorry about that, I was making assumptions in my message that the applet was already talking to the server through http. That is why I was focusing on the permissions of the 'web user' that was trying to write the file.
I hope I didn't mess anyone up.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, everything is fine. I appreciate your help. I think that these suggestions will put me on my way to getting up and running.
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!