Add new attachment

Only authorized users are allowed to upload new attachments.

This page (revision-7) was last changed on 29-Dec-2020 05:25 by krivacsz

This page was created on 29-Dec-2020 05:25 by Ben Spink

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Difference between version and

At line 1 added one line
Here is an example class that can interact with the server using HTTP to do download, upload, delete, rename, makedir, and list.
At line 5 changed 86 lines
public String login() throws Exception
{
//login and get an auth token
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setUseCaches(false);
urlc.setDoOutput(true);
urlc.getOutputStream().write(("command=login&username=demo&password=demo").getBytes("UTF8"));
urlc.getResponseCode();
String cookie = urlc.getHeaderField("Set-Cookie");
String auth = cookie.substring(cookie.indexOf("CrushAuth=")+"CrushAuth=".length(),cookie.indexOf(";",cookie.indexOf("CrushAuth=")));
urlc.disconnect();
return auth;
}
public void download(String auth, String serverPath, String localPath) throws Exception
{
//download a file using the auth token, and a post. Alternatively you could also just do a GET on the file.
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setDoInput(true);
urlc.setDoOutput(true);
urlc.getOutputStream().write(("command=download&path=/demo/KB2.txt").getBytes("UTF8"));
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
InputStream in = new BufferedInputStream(urlc.getInputStream());
RandomAccessFile out = new RandomAccessFile(localPath,"rw");
int bytesRead = 0;
byte b[] = new byte[32768];
while (bytesRead >= 0)
{
bytesRead = in.read(b);
if (bytesRead >= 0) out.write(b,0,bytesRead);
}
in.close();
out.close();
urlc.disconnect();
}
public void upload(String auth, String serverPath, String localPath) throws Exception
{
//upload a file using the auth token, and a PUT.
URL u = new URL("https://www.crushftp.com"+serverPath);
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("PUT");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setChunkedStreamingMode(32768);
urlc.setDoInput(true);
urlc.setDoOutput(true);
OutputStream out = urlc.getOutputStream();
RandomAccessFile in = new RandomAccessFile(localPath,"r");
int bytesRead = 0;
byte b[] = new byte[32768];
while (bytesRead >= 0)
{
bytesRead = in.read(b);
if (bytesRead >= 0) out.write(b,0,bytesRead);
}
in.close();
out.close();
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
urlc.disconnect();
}
public void doAction(String auth, String command, String param1, String param2) throws Exception
{
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setDoOutput(true);
if (command.equalsIgnoreCase("delete")) urlc.getOutputStream().write(("command=delete&names="+param1).getBytes("UTF8"));
else if (command.equalsIgnoreCase("rename")) urlc.getOutputStream().write(("command=rename&name1="+param1+"&name2="+param2).getBytes("UTF8"));
else if (command.equalsIgnoreCase("makedir")) urlc.getOutputStream().write(("command=makedir&path="+param1).getBytes("UTF8"));
else if (command.equalsIgnoreCase("getXMLListing")) urlc.getOutputStream().write(("command=getXMLListing&path="+param1).getBytes("UTF8"));
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
urlc.disconnect();
}
public String login(String username, String password) throws Exception
{
//login and get an auth token
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setUseCaches(false);
urlc.setDoOutput(true);
urlc.getOutputStream().write(("command=login&username="+username+"&password="+password).getBytes("UTF8"));
urlc.getResponseCode();
String cookie = urlc.getHeaderField("Set-Cookie");
String auth = cookie.substring(cookie.indexOf("CrushAuth=")+"CrushAuth=".length(),cookie.indexOf(";",cookie.indexOf("CrushAuth=")));
urlc.disconnect();
return auth;
}
public void download(String auth, String serverPath, String localPath) throws Exception
{
//download a file using the auth token, and a post. Alternatively you could also just do a GET on the file.
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setDoInput(true);
urlc.setDoOutput(true);
String c2f = auth.substring(auth.length() - 4);
urlc.getOutputStream().write(("command=download&path=/"+serverPath+"&c2f="+c2f).getBytes("UTF8"));
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
InputStream in = new BufferedInputStream(urlc.getInputStream());
RandomAccessFile out = new RandomAccessFile(localPath,"rw");
int bytesRead = 0;
byte b[] = new byte[32768];
while (bytesRead >= 0)
{
bytesRead = in.read(b);
if (bytesRead >= 0) out.write(b,0,bytesRead);
}
in.close();
out.close();
urlc.disconnect();
}
public void upload(String auth, String serverPath, String localPath) throws Exception
{
//upload a file using the auth token, and a PUT.
URL u = new URL("https://www.crushftp.com"+serverPath);
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("PUT");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setChunkedStreamingMode(32768);
urlc.setDoInput(true);
urlc.setDoOutput(true);
OutputStream out = urlc.getOutputStream();
RandomAccessFile in = new RandomAccessFile(localPath,"r");
int bytesRead = 0;
byte b[] = new byte[32768];
while (bytesRead >= 0)
{
bytesRead = in.read(b);
if (bytesRead >= 0) out.write(b,0,bytesRead);
}
in.close();
out.close();
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
urlc.disconnect();
}
public void doAction(String auth, String command, String param1, String param2) throws Exception
{
URL u = new URL("https://www.crushftp.com/");
HttpURLConnection urlc = (HttpURLConnection)u.openConnection();
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";");
urlc.setUseCaches(false);
urlc.setDoOutput(true);
String c2f = auth.substring(auth.length() - 4);
if (command.equalsIgnoreCase("delete")) urlc.getOutputStream().write(("command=delete&names="+param1+"&c2f="+c2f).getBytes("UTF8"));
else if (command.equalsIgnoreCase("rename")) urlc.getOutputStream().write(("command=rename&name1="+param1+"&name2="+param2+"&c2f="+c2f).getBytes("UTF8"));
else if (command.equalsIgnoreCase("makedir")) urlc.getOutputStream().write(("command=makedir&path="+param1+"&c2f="+c2f).getBytes("UTF8"));
else if (command.equalsIgnoreCase("getXMLListing")) urlc.getOutputStream().write(("command=getXMLListing&path="+param1+"&c2f="+c2f).getBytes("UTF8"));
int code = urlc.getResponseCode();
if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302;
if (code == 302) throw new Exception("Logged out.");
urlc.disconnect();
}
Version Date Modified Size Author Changes ... Change note
7 29-Dec-2020 05:25 4.459 kB krivacsz to previous
6 29-Dec-2020 05:25 4.394 kB Ben Spink to previous | to last
5 29-Dec-2020 05:25 4.524 kB Ben Spink to previous | to last
4 29-Dec-2020 05:25 4.293 kB Ben Spink to previous | to last
3 29-Dec-2020 05:25 4.294 kB Ben Spink to previous | to last
2 29-Dec-2020 05:25 3.955 kB Ben Spink to previous | to last
1 29-Dec-2020 05:25 2.293 kB Ben Spink to last
« This page (revision-7) was last changed on 29-Dec-2020 05:25 by krivacsz
G’day (anonymous guest)
CrushFTP10 | What's New

Referenced by
LeftMenu

JSPWiki