使用java写出图片的上传下载
时间: 2024-05-14 17:18:49 浏览: 73
图片上传:
```java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/uploadImage")
@MultipartConfig
public class UploadImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIR = "uploads";
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String applicationPath = request.getServletContext().getRealPath("");
String uploadPath = applicationPath + File.separator + UPLOAD_DIR;
File fileUploadDirectory = new File(uploadPath);
if (!fileUploadDirectory.exists()) {
fileUploadDirectory.mkdirs();
}
final Part filePart = request.getPart("image");
final String fileName = getFileName(filePart);
Path path = Paths.get(uploadPath + File.separator + fileName);
try (InputStream inputStream = filePart.getInputStream()) {
Files.copy(inputStream, path);
}
response.getWriter().print("Image uploaded successfully!");
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
for (String content : partHeader.split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(content.indexOf("=") + 2, content.length() - 1);
}
}
return null;
}
}
```
图片下载:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/downloadImage")
public class DownloadImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIR = "uploads";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("name");
String applicationPath = request.getServletContext().getRealPath("");
String downloadPath = applicationPath + File.separator + UPLOAD_DIR + File.separator + fileName;
File file = new File(downloadPath);
FileInputStream inputStream = new FileInputStream(file);
response.setContentType("image/jpeg");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
}
}
```
注意事项:
1. 这里使用的是 Servlet 3.0 的注解,所以需要在 web.xml 中添加 `<web-app version="3.0">`。
2. form 表单中的 `enctype` 必须是 `multipart/form-data`。
3. `getFileName` 方法可以获取上传文件的文件名。
4. `response.setContentType` 要设置成图片的 MIME 类型,如 `image/jpeg`。
5. `response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");` 中的 `filename` 参数可以指定下载时的文件名。
阅读全文