HTTP协议一次上传多个文件的方法

如何通过HTTP协议一次上传多个文件呢?在这里有两个思路,是同一个方法的两种实现。具体程序还需自己去设计

1. 在form中设置多个文件输入框,用数组命名他们的名字,如下:

1 < form action="" method="post" >
2 
3 < input name="usefile[]" type="file" >
4 
5 < input name="usefile[]" type="file" >
6 
7 < input name="usefile[]" type="file" >
8 
9 < /form >

这样,在服务器端做以下测试:

 1 echo " < pre > "; print_r($_FILES); echo " < /pre > "; 

2. 在form中设置多个文件输入框,但名字不同,如下:

1 < form action="" method="post" >
2 
3 < input name="usefile_a" type="file" >
4 
5 < input name="usefile_b" type="file" >
6 
7 < input name="usefile_c" type="file" >
8 
9 < /form >

在服务器端做同样测试:

 1 echo " < pre > "; print_r($_FILES); echo " < /pre > "; 

转载于:https://www.cnblogs.com/panxu/p/4730524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FTP 协议是文件传输协议,用于在网络上传文件。Java 通过 FTPClient 类提供了 FTP 客户端的实现,可以使用该类实现 FTP 文件上传。 下面是一个实现 FTP 协议多个文件上传的示例代码: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FTPUploader { private String server; private int port; private String user; private String password; private FTPClient ftpClient; public FTPUploader(String server, int port, String user, String password) { this.server = server; this.port = port; this.user = user; this.password = password; } public void connect() throws IOException { ftpClient = new FTPClient(); ftpClient.connect(server, port); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { throw new IOException("FTP server refused connection."); } boolean success = ftpClient.login(user, password); if (!success) { throw new IOException("Could not login to the FTP server."); } ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); } public void disconnect() throws IOException { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } public void uploadFiles(String directory, File[] files) throws IOException { if (!ftpClient.changeWorkingDirectory(directory)) { throw new IOException("Could not change working directory to " + directory); } for (File file : files) { String filename = file.getName(); try (FileInputStream inputStream = new FileInputStream(file)) { boolean success = ftpClient.storeFile(filename, inputStream); if (!success) { throw new IOException("Could not upload file " + filename); } } } } } ``` 使用该类的示例代码如下: ```java public static void main(String[] args) throws IOException { String server = "ftp.example.com"; int port = 21; String user = "username"; String password = "password"; String directory = "/upload"; File[] files = new File[] {new File("file1.txt"), new File("file2.txt"), new File("file3.txt")}; FTPUploader uploader = new FTPUploader(server, port, user, password); uploader.connect(); uploader.uploadFiles(directory, files); uploader.disconnect(); } ``` 以上代码实现了连接到 FTP 服务器,切换到指定目录,然后上传多个文件的操作。其中,`FTPClient` 类提供了很多与 FTP 协议相关的方法,可以根据需要进行调用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值