Java Web实现文件上传和下载接口功能详解
一、文件上传接口
1. HTML表单
文件上传的第一步是需要在前端实现一个HTML表单,用户通过这个表单可以上传文件。表单中必须含有一个type为file的input元素,这个元素用来选择文件。
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
2. 后端接收文件
表单提交后需要处理上传文件的接口来接收文件。在Java Web中,通过Servlet来处理HTTP请求是最常见的方式。处理上传文件需要注意的一点是要设置请求编码为multipart/form-data,因为这种编码支持文件上传。HttpServletRequest的getPart() 方法可以获取到表单中提交的所有文件,通过这个方法得到的每个Part对象都可以获取文件名、文件大小和文件内容。
public void doPost(HttpServletRequest request, HttpServletResponse response) {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Part part = request.getPart("file");
String fileName = part.getSubmittedFileName();
InputStream inputStream = part.getInputStream();
//处理文件操作
}
3. 文件保存
通过Part对象获取到文件内容后,就可以将文件保存到服务器本地。在保存文件之前,需要检查保存文件的目录是否存在,如果不存在则需要创建。这里以保存到Web应用程序的upload文件夹为例。
public void doPost(HttpServletRequest request, HttpServletResponse response) {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Part part = request.getPart("file");
String fileName = part.getSubmittedFileName();
InputStream inputStream = part.getInputStream();
String savePath = request.getServletContext().getRealPath("/") + "upload";
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
FileOutputStream outputStream = new FileOutputStream(new File(savePath, fileName));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
}
二、文件下载接口
1. 后端接口
文件下载的第一步实现后端下载接口。通过设置response的header头来告知浏览器下载文件,在这里需要设置Content-Disposition和Content-Type两个header头。Content-Disposition表示文件下载时的文件名,而Content-Type表示下载的文件类型。
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String fileName = "demo.jpg";
String filePath = request.getServletContext().getRealPath("/") + "upload/" + fileName;
File file = new File(filePath);
if (file.exists()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
}
}
2. 前端页面
在前端页面中添加一个超链接,并且这个链接指向后端下载接口就可以实现文件下载的功能了。在这里,我们以下载图片demo.jpg为例。
<a href="/download" target="_blank">下载图片</a>
示例一:文件上传代码示例
完整代码:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文件上传示例</title>
</head>
<body>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
</body>
</html>
public class FileUploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Part part = request.getPart("file");
String fileName = part.getSubmittedFileName();
InputStream inputStream = part.getInputStream();
String savePath = request.getServletContext().getRealPath("/") + "upload";
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
FileOutputStream outputStream = new FileOutputStream(new File(savePath, fileName));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
}
}
示例二:文件下载代码示例
完整代码:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文件下载示例</title>
</head>
<body>
<a href="/download" target="_blank">下载图片</a>
</body>
</html>
public class FileDownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String fileName = "demo.jpg";
String filePath = request.getServletContext().getRealPath("/") + "upload/" + fileName;
File file = new File(filePath);
if (file.exists()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java Web实现文件上传和下载接口功能详解 - Python技术站