java后台根据url下载world文档
时间: 2023-10-01 12:04:50 浏览: 49
以下是Java后台根据URL下载World文档的示例代码:
```
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadWordDocument {
public static void main(String[] args) {
String fileUrl = "http://example.com/document.docx"; // Replace with the URL of the Word document to download
String fileName = "document.docx"; // Replace with the desired file name
try {
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
connection.connect();
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close();
System.out.println("File downloaded successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
注意:此代码仅适用于Word文档的下载。如果要下载其他类型的文件,请相应地更改文件名和文件类型。此外,代码中的URL应该指向实际的Word文档的URL。