Java代码来从服务器下载图像到客户端

问题描述 投票:1回答:3

我是新到Java。我不很了解。我刚学习java。我开发一个Web应用程序。在我有一个选项,下载图像。如果用户点击他应该能够从服务器上下载图像客户端的发言权位置C://。

我已经实现这个代码::

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://。有谁能够告诉我要去哪里错了。

对于这个我使用净豆作为我的编辑器,Apache Tomcat作为服务器。客户端和服务器被通过端口未连接5051和image的客户希望从服务器下载是简单的JPG图片。会有人帮我摆脱这个问题。

java image download
3个回答
0
投票

如果该文件是越来越下载到C:\,那么这就是当你打开你的cpathFileOutputStream变量等于什么。这将意味着你的name变量被传递为空字符串。尝试把一些日志语句(甚至更好,使用NetBeans调试器!),看看有什么重视您的变量拿着的代码执行。

编辑:我想我现在明白这个问题。您正在运行这是一个servlet或类似的东西。这意味着你的代码在服务器上执行,而不是客户端。如果你想将文件下载到客户端上的特定的路径,你将不得不使用在客户端上运行的Applet或类似的东西。或者,您可以在HTTP响应返回文件和用户的浏览器会问他们在哪里保存文件。虽然,在这一点上,用户可以直接浏览到在浏览器本身的JPG。

您可能要更加详细地解释你的使用情况,如果这不回答你的问题。


0
投票

试试这个运行code.It会帮助你。

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();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.