downloading then running a uncommon file type on a mac

J

javajavalink

I need help with a program that will be run on mac OS X (with java
installed). The program has to download a file (not a common file type)
from a specific spot on aserver(running windows) to the desktop of the
mac. Then it has to open the file (as if you doulbe clicked it).

I am a relatively new to java. I know basic networking (i made a
chatroom program) and a bit about the regular stuff. I uses the sun
information (java.sun.com/j2se/1.5.0/docs/... alot so any refrences to
that would be helpful.

Here is what i don't know how to do (if it is possible in java):
-download a file from a server (is there a method for this that i can
use after i open the socket?)
-save this file to the desktop (this has to be done on a bunch of
different macs so i wont know their username or anything like that)
-launch the file (is there a program i can run on the file? like if is
was a MS word file, is there a program inside MS word that i could call
on the file?)

Thanks in advance!
 
S

Steve W. Jackson

javajavalink said:
I need help with a program that will be run on mac OS X (with java
installed). The program has to download a file (not a common file type)
from a specific spot on aserver(running windows) to the desktop of the
mac. Then it has to open the file (as if you doulbe clicked it).

I am a relatively new to java. I know basic networking (i made a
chatroom program) and a bit about the regular stuff. I uses the sun
information (java.sun.com/j2se/1.5.0/docs/... alot so any refrences to
that would be helpful.

Here is what i don't know how to do (if it is possible in java):
-download a file from a server (is there a method for this that i can
use after i open the socket?)
-save this file to the desktop (this has to be done on a bunch of
different macs so i wont know their username or anything like that)
-launch the file (is there a program i can run on the file? like if is
was a MS word file, is there a program inside MS word that i could call
on the file?)

Thanks in advance!

When you say "with Java installed" in reference to Mac OS X, it's kind
of redundant. Every copy of Mac OS X ever shipped has had Java
installed since its initial release in March 2001. Which version is
available, however, is a different matter. I don't know off the top if
10.3 systems have access to a Java 1.5 release or not. And 10.4 systems
do not have it installed by default. You might want to keep that in
mind.

I'll leave the networking for someone else to offer suggestions except
to say that it won't matter that the program is on a Mac.

As for the "desktop" question, that could depend on the language
setting. Mac OS X is build on a foundation that derives from BSD, so
it's got a *nix-style filesystem. Ordinarily, user home directories are
located in /Users, but you shouldn't rely on that. And the user's
desktop is represented by a directory named "Desktop" on US English
systems, but it may be named something else entirely in other language
settings. You'll find the Javadocs for Apple's extensions to Java at
<http://developer.apple.com/documentation/Java/Reference/1.5.0/appledoc/a
pi/index.html>. Take a look at the com.apple.eio.FileManager class
there. You probably need to use one of the static findFolder methods
there to do it safely. The trouble is, I'm having trouble locating a
clear idea what values you would need for domain (where you would
indicate the "user domain") and folderType (where you would indicate the
"desktop folder"). I'm only now beginning to delve into Mac-specific
issues for my own purposes, and I haven't figured that part out yet.

As for launching the file, you could take a relatively simple approach
and do something based on the Runtime.exec method. If you're familiar
with Windows, you know that you can use exec to "start" a file and let
Windows automatically hand it over to whatever app (if any) is
associated with its extension. Mac OS X doesn't rely entirely on
extensions, but you can do a similar trick there using the "open"
command. If you have access to a Mac OS X system, use its Terminal
application to get to a command line and try "man open" to read more.
Basically, though, the command "open file" (where file could also be a
URL, even a file: type URL) will open the file or URL with whatever
program the user has set for it. The man page will point out various
options for open.

= Steve =
 
T

Timo Stamm

Steve said:
And the user's
desktop is represented by a directory named "Desktop" on US English
systems, but it may be named something else entirely in other language
settings.

The directory is named "Desktop" in other language settings as well
(it's only translated in dialogs and the Finder).

There is one serious issue with filenames and non-ASCII characters. From
Wikipedia: "All UTF-8-MAC text is valid UTF-8 but precomposed characters
are forbidden and combining diacritics must be used to replace them. The
Mac OS X Operating System uses a special form of UTF-8 (sometimes
referred to as UTF-8-MAC) for writing file and folder names to the
filesystem."

Example: I created a file named "ä" in my Desktop directory. When I run
the following code

for (String f : new File("/Users/ts/Desktop/").list()) {
System.out.println(f);
}

I get "a?" as the file name.

new File("/Users/ts/Desktop/ä").exists()

however, returns true.


For this reason, I cannot commit files containing umlauts in their names
using the Eclipse CVS plugin.

That's truly "write once, debug everywhere".


Timo
 
J

javajavalink

Thanks very much.

Here is the code i have for connecting to the server using ftp:

import java.net.*;
import java.io.*;

public class URLtest
{
public static void main(String[] args)
{
int i = 0;
try
{
URL url = new URL("ftp://username:p[email protected]/test.sql");
System.out.println("1");
URLConnection urlc = url.openConnection();

FileOutputStream out = new FileOutputStream("E:\\file.sql");
System.out.println("Opened output");

BufferedReader in2 = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Opened input");

while ((i = in2.read()) >= 0)
{
out.write(i);
}
out.close();
in2.close();
System.out.println("Done");
}
catch (IOException f)
{
System.err.println("ERROR");
return;
}
}
}



It opens the URL fine but when it gets to "BufferedReader in2 =
new......", it throws and IOException.

Any ideas?
 
S

Steve W. Jackson

Timo Stamm said:
The directory is named "Desktop" in other language settings as well
(it's only translated in dialogs and the Finder).

There is one serious issue with filenames and non-ASCII characters. From
Wikipedia: "All UTF-8-MAC text is valid UTF-8 but precomposed characters
are forbidden and combining diacritics must be used to replace them. The
Mac OS X Operating System uses a special form of UTF-8 (sometimes
referred to as UTF-8-MAC) for writing file and folder names to the
filesystem."

Example: I created a file named "ä" in my Desktop directory. When I run
the following code

for (String f : new File("/Users/ts/Desktop/").list()) {
System.out.println(f);
}

I get "a?" as the file name.

new File("/Users/ts/Desktop/ä").exists()

however, returns true.


For this reason, I cannot commit files containing umlauts in their names
using the Eclipse CVS plugin.

That's truly "write once, debug everywhere".


Timo

I'm not overly familiar with Wikipedia. And when I looked up the
subject, I must say that I'm disappointed that they don't provide any
links supporting such a statement (they did provide such a link when
claiming that Java's DataInput uses a modified UTF-8). Nonetheless,
searching for it via Google confirms the use of a modified form of UTF-8
in the HFS filesystem usually used with Mac OS X.

What I don't understand is why you would get something like that from
Java. According to Sun's Javadocs, a Java String is UTF-16. And from a
recent exchange on the Apple Java-Dev mailing list concerning the
subject of pre- and decomposed characters, it's my impression that you
should not have seen that output from a println call. You may
understand it better than I should you care to visit the list archives
at <http://lists.apple.com/archives/Java-dev/2006>, where you'll find it
by the subject of "dnd of filenames with locale specific characters" in
the July messages.

I doubt that this would result in any troubles for transferring a file
from a Windows server to a Mac. Accepting that the user's desktop is
truly named "Desktop" in all language settings, I'll revise my earlier
recommendation to the OP and suggest using the "user.home" system
property to get the user's home directory, making a File object around
that and getting a new File object from the child "Desktop" directory.
Any file or directory created there would appear (in the Finder) on the
user's desktop.

= Steve =
 
S

Steve W. Jackson

javajavalink said:
Thanks very much.

Here is the code i have for connecting to the server using ftp:

import java.net.*;
import java.io.*;

public class URLtest
{
public static void main(String[] args)
{
int i = 0;
try
{
URL url = new URL("ftp://username:p[email protected]/test.sql");
System.out.println("1");
URLConnection urlc = url.openConnection();

FileOutputStream out = new FileOutputStream("E:\\file.sql");
System.out.println("Opened output");

BufferedReader in2 = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Opened input");

while ((i = in2.read()) >= 0)
{
out.write(i);
}
out.close();
in2.close();
System.out.println("Done");
}
catch (IOException f)
{
System.err.println("ERROR");
return;
}
}
}



It opens the URL fine but when it gets to "BufferedReader in2 =
new......", it throws and IOException.

Any ideas?

I see some code very similar to this in Sun's networking tutorial at
<http://java.sun.com/docs/books/tutorial/networking/urls/index.html>.
But the sample code deals only with reading via HTTP, or with reading
and writing via HTTP. I'm not familiar enough with the details of an
FTP connection to know whether the above code should work properly.

Of course, there should be *some* kind of clue, I would think, in the
stack trace. But you don't print it in the above code. Perhaps if you
add "f.printStackTrace();" in your catch block and read what it produces
you'll get an idea exactly what the problem is.

I also hope you're not planning to run the above on a Mac, since it
might end up creating a file with a very strange name in the directory
where you're running -- remembering that Macs don't have "drive letters".

= Steve =
 
J

javajavalink

import java.net.*;
import java.io.*;

public class URLtest
{
public static void main(String[] args) throws IOException
{
int i = 0;
try
{
URL url = new URL("ftp", "xx.xx.xx.xx", 20,
"username:password/test.sql");
System.out.println("1");
URLConnection urlc = url.openConnection();


FileOutputStream out = new FileOutputStream("E:\\file.sql");
System.out.println("Opened output");
BufferedReader in = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Opened input");

while ((i = in.read()) >= 0)
{
System.out.println("Got byte");
out.write(i);
}
out.close();
in.close();
System.out.println("Done");
}
catch (Exception f)
{
f.printStackTrace();
System.err.println("ERROR");
return;
}
}
}

I should have mentioned that i am using MS visual J# .NET 2003 student
edition. So i ran it with the printStackTrace() and here is what i got
(i included a copy of the program above). Line 18 is this line:

BufferedReader in = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));



1
Opened output
java.io.IOException:



at com.ms.vjsharp.protocol.ftp.VJSFtpURLConnection.connect()
at com.ms.vjsharp.protocol.ftp.VJSFtpURLConnection.getInputStream()
at URLtest.main(String[] args) in C:\Documents and Settings\Brian\My
Document
s\Visual Studio Projects\FTPtest\URLtest.java:line 18
ERROR
Press any key to continue
 
T

Timo Stamm

Steve said:
I'm not overly familiar with Wikipedia. And when I looked up the
subject, I must say that I'm disappointed that they don't provide any
links supporting such a statement (they did provide such a link when
claiming that Java's DataInput uses a modified UTF-8).

The fact is mentioned in various articles on apples developers sites,
but I haven't found a document that is dedicated to the issue. I guess
that makes it difficult to add a reference. And...

Nonetheless,
searching for it via Google confirms the use of a modified form of UTF-8
in the HFS filesystem usually used with Mac OS X.

it's a Unicode standard (http://www.unicode.org/reports/tr15/), not a
proprietary modified form of UTF-8 like in Java.

What I don't understand is why you would get something like that from
Java.

I don't have the slightest idea. Fortunately it's not a showstopper for me.

According to Sun's Javadocs, a Java String is UTF-16. And from a
recent exchange on the Apple Java-Dev mailing list concerning the
subject of pre- and decomposed characters, it's my impression that you
should not have seen that output from a println call. You may
understand it better than I should you care to visit the list archives
at <http://lists.apple.com/archives/Java-dev/2006>, where you'll find it
by the subject of "dnd of filenames with locale specific characters" in
the July messages.

Thanks for the link.

I doubt that this would result in any troubles for transferring a file
from a Windows server to a Mac.

I don't think so either. I was rather introducing my problem with
umlauts and CVS via Eclipse than solving the OPs problem.


Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top