📜  下载和上传图像

📅  最后修改于: 2020-12-14 05:35:38             🧑  作者: Mango


在本章中,我们将了解如何从Internet下载图像,对图像执行一些图像处理技术,然后再次将处理后的图像上传到服务器。

下载图像

为了从网站下载图像,我们使用名为URL的Java类,可以在java.net包下找到。其语法如下-

String website = "http://tutorialspoint.com";
URL url = new URL(website);                

除了上述方法,类URL中还有其他可用的方法,如下所述:

Sr.No. Method & Description
1

public String getPath()

It returns the path of the URL.

2

public String getQuery()

It returns the query part of the URL.

3

public String getAuthority()

It returns the authority of the URL.

4

public int getPort()

It returns the port of the URL.

5

public int getDefaultPort()

It returns the default port for the protocol of the URL.

6

public String getProtocol()

It returns the protocol of the URL.

7

public String getHost()

It returns the host of the URL.

以下示例演示如何使用Java URL类从互联网下载图像-

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.URL;

public class Download {

   public static void main(String[] args) throws Exception {
   
      try{
         String fileName = "digital_image_processing.jpg";
         String website = "http://tutorialspoint.com/java_dip/images/"+fileName;
         
         System.out.println("Downloading File From: " + website);
         
         URL url = new URL(website);
         InputStream inputStream = url.openStream();
         OutputStream outputStream = new FileOutputStream(fileName);
         byte[] buffer = new byte[2048];
         
         int length = 0;
         
         while ((length = inputStream.read(buffer)) != -1) {
            System.out.println("Buffer Read of length: " + length);
            outputStream.write(buffer, 0, length);
         }
         
         inputStream.close();
         outputStream.close();
         
      } catch(Exception e) {
         System.out.println("Exception: " + e.getMessage());
      }
   }
}

输出

当您执行上面给出的时,将看到以下输出。

下载和上传图像教程

它将从服务器下载以下图像。

下载和上传图像教程

上载图片

让我们看看如何将图像上传到Web服务器。我们将BufferedImage转换为字节数组,以便将其发送到服务器。

我们使用Java类ByteArrayOutputStream ,可以在java.io包下找到它。其语法如下-

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);

为了将图像转换为字节数组,我们使用ByteArrayOutputStream类的toByteArray()方法。其语法如下-

byte[] bytes = baos.toByteArray();

除了上述方法外,在ByteArrayOutputStream类中还有其他可用方法,如下所述:

Sr.No. Method & Description
1

public void reset()

This method resets the number of valid bytes of the byte array output stream to zero, so that all the accumulated output in the stream is discarded.

2

public byte[] toByteArray()

This method creates a newly allocated Byte array. Its size would be the current size of the output stream and the contents of the buffer will be copied into it. It returns the current contents of the output stream as a byte array.

3

public String toString()

Converts the buffer content into a string. Translation will be done according to the default character encoding. It returns the String translated from the buffer’s content.

4

public void write(int w)

It writes the specified array to the output stream.

5

public void write(byte []b, int of, int len)

It writes len number of bytes starting from offset off to the stream.

6

public void writeTo(OutputStream outSt)

It writes the entire content of this Stream to the specified stream argument.

以下示例演示ByteArrayOutputStream将图像上传到服务器-

客户代码

import javax.swing.*;  
import java.net.*; 
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Client{
   public static void main(String args[]) throws Exception{
   
      Socket soc;
      BufferedImage img = null;
      soc=new Socket("localhost",4000);
      System.out.println("Client is running. ");
      
      try {
         System.out.println("Reading image from disk. ");
         img = ImageIO.read(new File("digital_image_processing.jpg"));
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         
         ImageIO.write(img, "jpg", baos);
         baos.flush();
         
         byte[] bytes = baos.toByteArray();
         baos.close();
         
         System.out.println("Sending image to server. ");
         
         OutputStream out = soc.getOutputStream(); 
         DataOutputStream dos = new DataOutputStream(out);
         
         dos.writeInt(bytes.length);
         dos.write(bytes, 0, bytes.length);
         
         System.out.println("Image sent to server. ");

         dos.close();
         out.close();
         
      } catch (Exception e) {
         System.out.println("Exception: " + e.getMessage());
         soc.close();
      }
      soc.close();
   }
}

服务器代码

import java.net.*;
import java.io.*;
import java.awt.image.*;

import javax.imageio.*; 
import javax.swing.*; 

class Server {
   public static void main(String  args[]) throws Exception{
      ServerSocket server=null;
      Socket socket;
      server = new ServerSocket(4000);
      System.out.println("Server Waiting for image");

      socket = server.accept();
      System.out.println("Client connected.");
      
      InputStream in = socket.getInputStream();
      DataInputStream dis = new DataInputStream(in);

      int len = dis.readInt();
      System.out.println("Image Size: " + len/1024 + "KB");
      
      byte[] data = new byte[len];
      dis.readFully(data);
      dis.close();
      in.close();

      InputStream ian = new ByteArrayInputStream(data);
      BufferedImage bImage = ImageIO.read(ian);
 
      JFrame f = new JFrame("Server");
      ImageIcon icon = new ImageIcon(bImage);
      JLabel l = new JLabel();
      
      l.setIcon(icon);
      f.add(l);
      f.pack();
      f.setVisible(true);
   }
}

输出

客户端输出

当执行客户端代码时,以下输出将出现在客户端-

下载上传图片

服务器端输出

当您执行服务器代码时,以下输出将出现在服务器端-

下载上传图片

收到图像后,服务器显示图像,如下所示:

下载上传图片