@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
InputStream fileContent = filePart.getInputStream();
// 假设这里有一个方法用于保存文件到服务器
saveFile(fileName, fileContent);
}
}
]]>
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getParameter("filename");
File file = new File("/path/to/files/" + fileName);
if (file.exists()) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
}
]]>
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!