我很抱歉,如果这可能与其他人有点类似的问题,但我找不到我正在寻找的答案,而且我有点匆忙。 我按照教程here来创建文件上传servlet。
但是我需要创建一个你也可以从中下载和删除的(通过使用首次上传时给出的文件特定键)。虽然我仍然像疯了似的疯狂,并试图围绕一些(非常不同的)教程包围,有没有人在这里可以帮助像我这样的菜鸟修改上面提到的教程中的servlet我的东西我能理解/合作吗? (我之前从未使用过JSP& Servlet :()
答案 0 :(得分:0)
我设法完成了服务器以满足我的需求。 我使用this教程进行上传;和this下载教程。 根据我从这两个中学到的东西,我设法创建了一个用于从服务器删除数据文件的类:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private static final int BUFSIZE = 4096;
private String filePath;
private String filename;
public void init() {
// the file data.xls is under web application folder
log(" filepath = "+filePath);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
filename = request.getParameter("filename");
System.out.println("filename = "+filename);
filePath = getServletContext().getRealPath("") + File.separator+"data"+File.separator + filename;
File file = new File(filePath);
file.delete();
getServletContext().getRequestDispatcher("/deleted.jsp").forward(request, response);
}
}
接下来是我的upload.jsp文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
Upload----------------------------------------------------------------------------------
<form method="post" action="UploadServlet" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />
</form>
Download-------------------------------------------------------------------------------- --------------
<form action="DownloadServlet" method="get">
File Name: <input type="text" name="filename"/>
<input type="submit" value="Download" />
<a href="DownloadServlet"></a>
</form>
Delete---------------------------------------------------------------------------------- ------------
<form action="DeleteServlet" method="get">
File Name: <input type="text" name="filename"/>
<input type="submit" value="Delete" />
<a href="DownloadServlet"></a>
</form>
最后我的web.xml(可能有一些来自其他服务器尝试的剩余代码,但它工作正常):
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CodeWeb</display-name>
<servlet>
<description></description>
<display-name>UploadServlet</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.upload.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>DownloadServlet</display-name>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>com.upload.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/DownloadServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DeleteServlet</display-name>
<servlet-name>DeleteServlet</servlet-name>
<servlet-class>com.upload.DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteServlet</servlet-name>
<url-pattern>/DeleteServlet</url-pattern>
</servlet-mapping>
</web-app>
希望这有助于某人。