用Java写的程序怎么发送
在Java编程中,发送数据或请求通常涉及网络通信、文件传输、邮件发送等多种场景,以下是一些常见的“发送”操作及其实现方法的详细解答。

发送HTTP请求
使用HttpURLConnection发送GET请求
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class SendHttpGet {
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用HttpURLConnection发送POST请求
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class SendHttpPost {
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
String jsonInputString = "{"name": "John", "age": 30}";
try(OutputStream os = conn.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
发送电子邮件
使用JavaMail API发送邮件

import javax.mail.;
import javax.mail.internet.;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) {
final String username = "your_email@example.com";
final String password = "your_password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.example.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from_email@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to_email@example.com"));
message.setSubject("Test Email");
message.setText("This is a test email sent from Java program");
Transport.send(message);
System.out.println("Email sent successfully!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
发送文件到FTP服务器
使用Apache Commons Net库发送文件到FTP服务器
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class SendFileToFTP {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String user = "username";
String pass = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = new FileInputStream("localfile.txt");
boolean done = ftpClient.storeFile("/remote/path/file.txt", inputStream);
inputStream.close();
if (done) {
System.out.println("File uploaded successfully.");
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
FAQs
Q1: 如何在Java中发送HTTP请求?
A1: 可以使用HttpURLConnection类来发送HTTP请求,对于GET请求,可以直接打开连接并读取响应;对于POST请求,需要设置doOutput为true,并通过输出流发送数据。
Q2: 如何使用Java发送电子邮件?
A2: 可以使用JavaMail API来发送电子邮件,需要配置SMTP服务器的属性,创建会话,构建邮件消息,并使用`Transport.

原创文章,发布者:酷盾叔,转转请注明出处:https://www.kd.cn/ask/98646.html