2012-02-06 63 views
1

我是新來的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圖像。任何人都可以幫助我擺脫這個問題。

+0

你提到你有問題,但疏於說他們具體是什麼。我也很難相信有人會投這個問題。 – 2012-02-06 08:43:09

+0

如果這是一個學習的情況,你應該把你的問題標記爲家庭作業。 – 2012-02-06 08:43:55

+0

抱歉給我帶來了不便,我更新了我的問題。問題是,它被下載到服務器端在C://而不是客戶端 – kanchan 2012-02-06 08:46:14

回答

0

如果文件被下載到C:\,那麼這就是當您打開FileOutputStreamcpath變量等於的值。這意味着你的name變量被作爲空字符串傳入。嘗試使用一些日誌語句(或者更好,使用netbeans調試器!)來查看您的代碼執行時變量持有的值。

編輯:我想我現在明白了這個問題。你正在運行它作爲一個servlet或類似的東西。這意味着您的代碼正在服務器上執行,而不是在客戶端上執行。如果要將文件下載到客戶端上的特定路徑,則必須使用在客戶端運行的Applet或類似的東西。或者,您可以在HTTP響應中返回文件,用戶的瀏覽器會詢問他們保存文件的位置。雖然在這一點上,用戶可以在瀏覽器中自己導航到jpg。

如果這不能回答你的問題,你可能想更詳細地解釋你的用例。

+0

我調試我的程序名稱是圖像的名稱,它是正確的。我只想讓它下載到客戶端而不是服務器端。那我該怎麼辦? – kanchan 2012-02-06 08:56:52

+0

@ user1191852查看我的更新回覆。我想我現在明白你的問題了! – Jon7 2012-02-06 09:06:54

+0

先生,我可以得到這樣的程序的代碼。我的截止日期已關閉 – kanchan 2012-02-06 09:10:50

0

試試這個正在運行的代碼。它會幫助你。

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.Authenticator; 
import java.net.PasswordAuthentication; 
import java.net.URL; 

public class SaveImageFromUrl { 

    public static void main(String[] args) throws Exception { 



     String imageUrl = "http://2.bp.blogspot.com/_GHaEnqqbRsE/SVsxi-gdQ2I/AAAAAAAAAAU/NS6MEejoHtE/s320/Gppfront.jpg"; 


     String destinationFile = "D://gpp.jpg"; 

     saveImage(imageUrl, destinationFile); 
    } 

    public static void saveImage(String imageUrl, String destinationFile) throws IOException { 
     URL url = new URL(imageUrl); 
     InputStream is = url.openStream(); 
     OutputStream os = new FileOutputStream(destinationFile); 

     byte[] b = new byte[2048]; 
     int length; 

     while ((length = is.read(b)) != -1) { 
      os.write(b, 0, length); 
     } 

     is.close(); 
     os.close(); 
    } 

} 
0

請首先嚐試這方面的工作代碼:

package com.ashugupt.github.stackover; 

import java.io.BufferedReader; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class URLTest { 

    private static void sendGet() throws Exception { 

    String url = "http://www.uni-koblenz-landau.de/images/starts-c-ko.jpg"; 
    URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    // optional default is GET 
    con.setRequestMethod("GET"); 

    //add request header 
    con.setRequestProperty("User-Agent", "Mozilla/5.0"); 

    int responseCode = con.getResponseCode(); 
    System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response Code : " + responseCode); 

    InputStream in = con.getInputStream(); 
    OutputStream out = new FileOutputStream("/Users/ravikiran/Desktop/abc.jpg"); 
    try { 
     byte[] bytes = new byte[2048]; 
     int length; 

     while ((length = in.read(bytes)) != -1) { 
     out.write(bytes, 0, length); 
     } 
    } finally { 
     in.close(); 
     out.close(); 
    } 
    } 

    public static void main(String[] args) throws Exception { 
    sendGet(); 
    } 
} 
相關問題