java 传输文件实现快速、安全的数据交换

Java可以使用java.net包中的URLConnection类来传输文件。下面是一个使用URLConnection传输文件的示例代码:

Java可以使用java.net包中的URLConnection类来传输文件。是一个使用URLConnection传输文件的示例代码:

Java可以使用java.net包中的URLConnection类来传输文件。是一个使用URLConnection传输文件的示例代码:

import java.io.*;

import java.net.*;

public class FileTransfer {

public static void main(String[] args) {

try {

// 创建URL实例

URL url = new URL("http://www.example.com/files/file.txt");

// 打开URL连接

URLConnection connection = url.openConnection();

// 设置请求属性

connection.setRequestProperty("Accept-Cht", "UTF-8");

connection.setRequestProperty("Content-Type", "text/plain; cht=utf-8");

// 获取输入流

InputStream in = connection.getInputStream();

// 将输入流转换为字节数组

byte[] bytes = new byte[1024];

int len;

ByteArrayOutputStream out = new ByteArrayOutputStream();

while ((len = in.read(bytes)) != -1) {

out.write(bytes, 0, len);

}

// 将字节数组转换为字符串

String result = new String(out.toByteArray(), "utf-8");

System.out.println(result);

// 关闭输入流

in.close();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(238)
python 趣味编程:如何使用Python来让你的日常生活变得更有趣!
上一篇
python使用:如何使用Python来提高编程效率
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(52条)