我是新來的java。我對此不太瞭解。我只是在學習java。 我正在開發一個Web應用程序。其中我有一個選項下載圖像。如果用戶點擊,他應該能夠從服務器下載圖像到客戶端說在位置c://。將代碼從服務器下載到客戶端的java代碼
我已經實現了這個代碼::
import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class DownloadingImages{
public DownloadingImages() {}
public void download(String name) throws MalformedURLException, IOException{
Image image = null;
try {
//URL url = new URL("file:///E:/myproject/build/web/images/Webcam.jpg");
String spath="http://localhost:5051/marketpoint/images/";
String cpath="C:\\";
spath = spath + name ;
cpath = cpath + name ;
System.out.println("FULL path::: "+spath);
URL url = new URL(spath);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(cpath);
fos.write(response);
fos.close();
} catch (IOException e) {
}
}
}
Here
name = name of image thta client wants to download.
這裏的問題是,圖像被下載到服務器端。在c://。任何人都可以告訴我哪裏錯了。
爲此,我使用net bean作爲我的編輯器,Apache tomcat作爲服務器。客戶端和服務器通過端口號5051連接。客戶端要從服務器上下載的圖像是簡單的jpg圖像。任何人都可以幫助我擺脫這個問題。
你提到你有問題,但疏於說他們具體是什麼。我也很難相信有人會投這個問題。 – 2012-02-06 08:43:09
如果這是一個學習的情況,你應該把你的問題標記爲家庭作業。 – 2012-02-06 08:43:55
抱歉給我帶來了不便,我更新了我的問題。問題是,它被下載到服務器端在C://而不是客戶端 – kanchan 2012-02-06 08:46:14