在Debian系統上使用JSP(JavaServer Pages)實現文件上傳和下載,通常需要以下幾個步驟:
安裝必要的軟件:
sudo apt update
sudo apt install tomcat9
配置Tomcat:
sudo systemctl status tomcat9
sudo systemctl start tomcat9
創建JSP文件:
fileUploadDownload
。WEB-INF
目錄,并在其中創建一個web.xml
文件來配置Servlet。編寫JSP文件:
index.jsp
文件來提供文件上傳表單。upload.jsp
文件來處理文件上傳。download.jsp
文件來處理文件下載。以下是一個簡單的示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h1>Upload File</h1>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
<%@ page import="java.io.*, java.util.*, javax.servlet.*, javax.servlet.http.*, org.apache.commons.fileupload.*, org.apache.commons.fileupload.disk.*, org.apache.commons.io.output.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>File Upload Status</title>
</head>
<body>
<%
String uploadPath = application.getRealPath("") + "uploads";
File fileSaveDir = new File(uploadPath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.println("Error: Form must has enctype=multipart/form-data.");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> formItems = null;
try {
formItems = upload.parseRequest(request);
} catch (Exception ex) {
out.println("There was an error: " + ex.getMessage());
}
if (formItems != null && formItems.size() > 0) {
for (FileItem item : formItems) {
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
item.write(storeFile);
out.println("Uploaded file name: " + fileName + "<br>");
out.println("File size: " + item.getSize() + "<br>");
}
}
}
%>
<a href="index.jsp">Upload another file</a>
</body>
</html>
<%@ page import="java.io.*, java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>File Download</title>
</head>
<body>
<%
String fileName = request.getParameter("fileName");
if (fileName == null || fileName.isEmpty()) {
out.println("File name is required.");
return;
}
String filePath = application.getRealPath("") + "uploads" + File.separator + fileName;
File file = new File(filePath);
if (!file.exists()) {
out.println("File not found.");
return;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
FileInputStream inStream = new FileInputStream(file);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.flush();
%>
<a href="index.jsp">Back to upload page</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>UploadServlet</servlet-name>
<jsp-file>/upload.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/upload.jsp</url-pattern>
</servlet-mapping>
</web-app>
uploads
目錄存在并且有寫權限。通過以上步驟,你可以在Debian系統上使用JSP實現文件上傳和下載功能。