Java下载文件加水印
在日常开发中,我们经常需要从网络上下载文件,并且有时候还需要对下载的文件进行一些处理,比如给文件添加水印。本文将介绍如何使用Java来下载文件并给文件添加水印。
下载文件
首先,我们需要从网络上下载文件。Java提供了许多方法来实现文件下载,其中一种常用的方式是使用URLConnection
类。下面是一个简单的示例代码:
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class FileDownloader {
public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
fileOutputStream.close();
bufferedInputStream.close();
inputStream.close();
}
}
上面的代码通过URL
和URLConnection
类来连接到指定的文件URL,并将文件内容写入到指定的保存路径中。使用该方法可以下载任意类型的文件。
添加水印
接下来,我们需要给下载的文件添加水印。水印是一种在文件上叠加的透明文字或图片,用于标识文件的来源、版权信息等。在Java中,可以使用第三方库Apache PDFBox
来实现给PDF文件添加水印。下面是一个示例代码:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.io.IOException;
public class PdfWatermarker {
public static void addWatermark(String filePath, String watermarkText, String outputFilePath) throws IOException {
PDDocument document = PDDocument.load(new File(filePath));
for (PDPage page : document.getPages()) {
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 48);
contentStream.setNonStrokingColor(255, 0, 0);
PDRectangle pageSize = page.getMediaBox();
float x = (pageSize.getLowerLeftX() + pageSize.getUpperRightX()) / 2;
float y = (pageSize.getLowerLeftY() + pageSize.getUpperRightY()) / 2;
contentStream.beginText();
contentStream.showTextAligned(watermarkText, x, y, PDPageContentStream.Alignment.CENTER, 45);
contentStream.endText();
contentStream.close();
}
document.save(outputFilePath);
document.close();
}
}
上面的代码使用PDDocument
和PDPageContentStream
类来操作PDF文件,通过设置字体、颜色、位置等信息来添加水印。使用该方法可以给任意PDF文件添加水印。
示例代码
下面是一个完整的示例代码,演示了如何下载文件并给文件添加水印:
public class Main {
public static void main(String[] args) {
try {
// 下载文件
String fileUrl = "
String savePath = "path/to/save/file.pdf";
FileDownloader.downloadFile(fileUrl, savePath);
// 添加水印
String filePath = "path/to/save/file.pdf";
String watermarkText = "Watermark";
String outputFilePath = "path/to/save/watermarked_file.pdf";
PdfWatermarker.addWatermark(filePath, watermarkText, outputFilePath);
System.out.println("文件下载并添加水印成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上面的示例代码中,我们首先调用downloadFile
方法下载文件,然后调用addWatermark
方法给下载的文件添加水印。最后,程序将输出"文件下载并添加水印成功!"。
总结
本文介绍了如何使用Java下载文件并给文件添加水印。通过使用URLConnection
类来下载文件,使用Apache PDFBox
库来给PDF文件添加水印,我们可以方便地实现这些功能。