Hallo,
Also der Upload funktioniert soweit, aber das Problem ist, dass die Datei auf dem Server eine Größe von 0Bytes hat. Ich habe dazu folgenden Code:
Habt ihr eine Idee an was das liegen kann, da ich auch keine Fehlermeldung bekomme.
PS.: Der Hinweis, dass ich die Apache Commons Net Bibliothek nutzen könnte ist zwar schön, das kann ich aber nicht machen, da der Port 21 nicht benutzt werden kann. Warum auch immer. Das konnte mir keiner sagen. Und einen anderen Port kann man nicht aufmachen, da der Server diese Möglichkeit nicht hergibt.
Danke
Also der Upload funktioniert soweit, aber das Problem ist, dass die Datei auf dem Server eine Größe von 0Bytes hat. Ich habe dazu folgenden Code:
Java:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
class FTPConnection {
private Proxy proxy;
private String server;
private final int TIMEOUT_MS;
public FTPConnection(String proxyHost, int proxyPort, String server, String user, String password, int timeout) {
this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
this.server = server;
this.TIMEOUT_MS = timeout;
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
});
}
public boolean upload(String remote, File local) {
try {
URLConnection urlConnection = createURLConnection(remote, false);
write(new FileInputStream(local), urlConnection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private void write(InputStream streamInput, OutputStream streamOutput) throws IOException {
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(streamInput);
output = new BufferedOutputStream(streamOutput);
byte[] bytes = new byte[4096];
for(int read = 0; (read = input.read(bytes)) != -1;) {
output.write(bytes, 0, read);
}
output.flush();
} finally {
try {
if(input != null){
input.close();
}
if(output != null){
output.close();
}
} catch (Exception e2) {
//do nothing
}
}
}
private URLConnection createURLConnection(String remote, boolean doInput) throws IOException {
URL url = new URL(server+remote);
URLConnection urlConnection = url.openConnection(proxy);
urlConnection.setDoInput(doInput);
urlConnection.setDoOutput(!doInput);
urlConnection.setConnectTimeout(TIMEOUT_MS);
urlConnection.setReadTimeout(TIMEOUT_MS);
urlConnection.connect();
return urlConnection;
}
}
Habt ihr eine Idee an was das liegen kann, da ich auch keine Fehlermeldung bekomme.
PS.: Der Hinweis, dass ich die Apache Commons Net Bibliothek nutzen könnte ist zwar schön, das kann ich aber nicht machen, da der Port 21 nicht benutzt werden kann. Warum auch immer. Das konnte mir keiner sagen. Und einen anderen Port kann man nicht aufmachen, da der Server diese Möglichkeit nicht hergibt.
Danke