java图片传输_JAVA中传输图片的示例 | 学步园

利用JAVA中的字节流传输图片

服务端代码

Server

package net;

import java.io.BufferedInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class DataSocketServer {

final public static int DEFAULT_PORT = 4848;

final public static String FILE_DIR = "D:/";

public static void main(String[] args) {

ServerSocket server = null;

try {

server = new ServerSocket(DEFAULT_PORT);

while (true) {

new Thread(new RequestProcessorTask(server.accept())).start();

}

} catch (IOException e) {

e.printStackTrace();

}

}

static class RequestProcessorTask implements Runnable {

private Socket socket = null;

public RequestProcessorTask(Socket socket) {

this.socket = socket;

}

public void run() {

try {

boolean isEnd = false;

BufferedInputStream in = new BufferedInputStream(socket.getInputStream());

while (!isEnd) {

int d = -1;

StringBuilder header = new StringBuilder();

while ((d = in.read()) != '\r') {

if (d == -1) {

isEnd = true;

break;

}

header.append((char) d);

}

if (!isEnd) {

String[] parms = header.toString().split(";");

FileOutputStream out = new FileOutputStream(FILE_DIR + parms[0]);

long size = Long.parseLong(parms[1]);

while (size > 0 && (d = in.read()) != -1) {

out.write(d);

size--;

}

out.flush();

out.close();

}

}

in.close();

} catch (IOException e) {

throw new RuntimeException("获取客户端输入流失败", e);

}

}

}

}

客户端代码

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.Socket;

public class DataSocket {

public static void main(String[] args) throws IOException

{

File img1 = new File("e:/a.png");

File img2 = new File("e:/b.png");

Socket socket = new Socket("127.0.0.1", 4848);

String header1 = "a.png;" + img1.length();

String header2 = "b.png;" + img2.length();

BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

FileInputStream in = new FileInputStream(img1);

FileInputStream in2 = new FileInputStream(img2);

byte[] buffer = new byte[8192];

int readBytes = -1;

out.write(header1.getBytes());

out.write("\r".getBytes());

out.flush();

while ((readBytes = in.read(buffer)) != -1)

{

out.write(buffer, 0, readBytes);

}

out.flush();

out.write(header2.getBytes());

out.write("\r".getBytes());

out.flush();

while ((readBytes = in2.read(buffer)) != -1)

{

out.write(buffer, 0, readBytes);

}

out.flush();

in.close();

in2.close();

out.flush();

out.close();

}

}

单次图片发送可以这样使用,不过我在代码中同时传数据和图片就会出现问题。

问题原因是:

传图片的用的也是建立的socket通道,在每次传完图片后关闭socket连接,再使用该socket就会出错。

解决办法:

在发送玩图片后发送一个结束标志给接收端,接收端接收到这个接收标志就结束接收。接收端可以实现一次接收多张图片,直到接收到结束标志再结束,发送端一次发送一张图片。

优化后代码如下:

//接收图片

public void receive_picture()

{

try {

boolean isEnd = false;

BufferedInputStream in = new BufferedInputStream(socket.getInputStream());

while (!isEnd) {

int d = -1;

StringBuilder header = new StringBuilder();

while ((d = in.read()) != '\r') {

if (d == -1) {

isEnd = true;

break;

}

header.append((char) d);

}

if (!isEnd) {

String[] parms = header.toString().split(";");

if(parms[1].equals("end"))

{

isEnd = true;

break;

}

FileOutputStream out = new FileOutputStream("E:/image/"+ parms[0]);

System.out.println("我的天: "+"E:/image/"+parms[0]);

long size = Long.parseLong(parms[1]);

while (size > 0 && (d = in.read()) != -1) {

out.write(d);

size--;

}

out.flush();

out.close();

}

}

//in.close();

} catch (IOException e) {

throw new RuntimeException("获取客户端输入流失败", e);

}

}

//发送图片

public void send_picture(String path)

{

byte[] buffer = new byte[8192];

int readBytes = -1;

try

{

File image = new File(path);

FileInputStream in = new FileInputStream(path);

String parms[] = path.split("/");

String header = parms[2]+";"+image.length();

out.write(header.getBytes());

out.write("\r".getBytes());

out.flush();

while ((readBytes = in.read(buffer)) != -1)

{

out.write(buffer, 0, readBytes);

}

out.flush();

in.close();

//System.out.println("发送成功?");

}

catch(FileNotFoundException e)

{}

catch(IOException e)

{}

}

发送完后发送接收标志

for(int j=0;j

{

send_picture(seller[j].Seller_imagePath);

}

out.write("end;end".getBytes());

out.write("\r".getBytes());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,这篇文章介绍了如何使用Java编写爬虫程序来获取邮件信息。这里提供了一个完整的示例代码,可以帮助你更好地理解如何实现邮件爬虫。 以下是示例代码: ```java import java.io.IOException; import java.util.Properties; import javax.mail.Address; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; public class EmailCrawler { public static void main(String[] args) throws MessagingException, IOException { // 邮箱账号配置 String host = "pop3.example.com"; String username = "[email protected]"; String password = "your_password"; // 连接到邮件服务器 Properties props = new Properties(); props.setProperty("mail.store.protocol", "pop3"); props.setProperty("mail.pop3.host", host); Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect(username, password); // 获取收件箱 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); // 遍历邮件 Message[] messages = inbox.getMessages(); for (Message message : messages) { Address[] froms = message.getFrom(); String from = ((InternetAddress) froms[0]).getAddress(); String subject = message.getSubject(); String date = message.getSentDate().toString(); System.out.printf("From: %s%nSubject: %s%nDate: %s%n", from, subject, date); System.out.println("------------------------"); } // 关闭连接 inbox.close(false); store.close(); } } ``` 在上面的代码,我们使用JavaMail API连接到指定的POP3邮件服务器,并获取收件箱。然后,我们遍历所有邮件,并提取出邮件的发件人、主题和日期。最终,我们打印出每个邮件的相关信息。 在使用时,我们需要将代码的`host`、`username`和`password`替换为自己的邮箱账号信息。同时,我们还需要将代码的`pop3.example.com`替换为自己的POP3邮件服务器地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值