Bir Dosyayı Sunucuya POST Eden Java Applet
POST edilecek php dosyası:
<?php
if(!empty($_FILES)) {
include ("__klas.php");
$FN = new __klas();
echo $_FILES['dosya']['name'];
$FTPBaglantisi=$FN->FTP();
if($FTPBaglantisi) {
ftp_put($FTPBaglantisi, __klas::FTPDIZIN."/".$_FILES['dosya']['name'], $_FILES['dosya']['tmp_name'], FTP_BINARY);
}
}
?>
if(!empty($_FILES)) {
include ("__klas.php");
$FN = new __klas();
echo $_FILES['dosya']['name'];
$FTPBaglantisi=$FN->FTP();
if($FTPBaglantisi) {
ftp_put($FTPBaglantisi, __klas::FTPDIZIN."/".$_FILES['dosya']['name'], $_FILES['dosya']['tmp_name'], FTP_BINARY);
}
}
?>
Yukardaki php dosyasına herhangi bir resim dosyasını post eden java applet:
import java.applet.Applet;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.net.URLConnection;
import java.net.URL;
import java.util.Random;
public class poster5 extends Applet {
private final String CrLf = "rn";
protected static String randomString() {
return Long.toString(new Random().nextLong(), 36);
}
private void httpConn() {
URLConnection conn = null;
OutputStream os = null;
InputStream is = null;
try {
URL url = new URL("http://www.hebelehubele.com/test.php");
System.out.println("url:" + url);
conn = url.openConnection();
conn.setDoOutput(true);
//InputStream imgIs = getClass().getResourceAsStream("1010798.JPG");
//File f = new File("/home/ret/Desktop", "1010798.JPG");
//InputStream imgIs = new FileInputStream(f);
InputStream imgIs = new FileInputStream("/home/ret/Desktop/1010798.JPG");
byte[] imgData = new byte[imgIs.available()];
imgIs.read(imgData);
String Ayrac = "---------------------------" + randomString() + randomString() + randomString();
String message1 = "";
message1 += "--" + Ayrac + CrLf;
message1 += "Content-Disposition: form-data; name="dosya"; filename="1010798.JPG"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf;
message1 += CrLf;
// the image is sent between the messages in the multipart message.
String message2 = "";
message2 += CrLf + "--" + Ayrac + "--" + CrLf;
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Ayrac);
// might not need to specify the content-length when sending chunked data.
conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));
//System.out.println("open os");
os = conn.getOutputStream();
//System.out.println(message1);
os.write(message1.getBytes());
// SEND THE IMAGE
int index = 0;
int size = 2048;
do {
//System.out.println("write:" + index);
if ((index + size) > imgData.length) {
size = imgData.length - index;
}
os.write(imgData, index, size);
index += size;
} while (index < imgData.length);
//System.out.println("written:" + index);
//System.out.println(message2);
os.write(message2.getBytes());
os.flush();
//System.out.println("open is");
is = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
//System.out.println("Close connection");
try {
os.close();
} catch (Exception e) {
}
try {
is.close();
} catch (Exception e) {
}
}
}
public void init() {
httpConn();
}
// TODO overwrite start(), stop() and destroy() methods
}
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.net.URLConnection;
import java.net.URL;
import java.util.Random;
public class poster5 extends Applet {
private final String CrLf = "rn";
protected static String randomString() {
return Long.toString(new Random().nextLong(), 36);
}
private void httpConn() {
URLConnection conn = null;
OutputStream os = null;
InputStream is = null;
try {
URL url = new URL("http://www.hebelehubele.com/test.php");
System.out.println("url:" + url);
conn = url.openConnection();
conn.setDoOutput(true);
//InputStream imgIs = getClass().getResourceAsStream("1010798.JPG");
//File f = new File("/home/ret/Desktop", "1010798.JPG");
//InputStream imgIs = new FileInputStream(f);
InputStream imgIs = new FileInputStream("/home/ret/Desktop/1010798.JPG");
byte[] imgData = new byte[imgIs.available()];
imgIs.read(imgData);
String Ayrac = "---------------------------" + randomString() + randomString() + randomString();
String message1 = "";
message1 += "--" + Ayrac + CrLf;
message1 += "Content-Disposition: form-data; name="dosya"; filename="1010798.JPG"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf;
message1 += CrLf;
// the image is sent between the messages in the multipart message.
String message2 = "";
message2 += CrLf + "--" + Ayrac + "--" + CrLf;
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Ayrac);
// might not need to specify the content-length when sending chunked data.
conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));
//System.out.println("open os");
os = conn.getOutputStream();
//System.out.println(message1);
os.write(message1.getBytes());
// SEND THE IMAGE
int index = 0;
int size = 2048;
do {
//System.out.println("write:" + index);
if ((index + size) > imgData.length) {
size = imgData.length - index;
}
os.write(imgData, index, size);
index += size;
} while (index < imgData.length);
//System.out.println("written:" + index);
//System.out.println(message2);
os.write(message2.getBytes());
os.flush();
//System.out.println("open is");
is = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
//System.out.println("Close connection");
try {
os.close();
} catch (Exception e) {
}
try {
is.close();
} catch (Exception e) {
}
}
}
public void init() {
httpConn();
}
// TODO overwrite start(), stop() and destroy() methods
}
Yorumunuzu Ekleyin