0

如果这可能是与其他人有些相似的问题,我深表歉意,但我还没有找到我正在寻找的答案,而且我有点着急。我已按照此处看到的教程创建文件上传 servlet。

但我需要创建一个,您也可以从中下载和删除。(通过使用第一次上传时给出的文件特定密钥)。虽然我还在疯狂地搜索谷歌并试图围绕一些(非常不同的)教程来思考,但这里有没有人可以帮助像我这样的菜鸟将上述教程中的 servlet 修改为我可以理解/合作吗?(我以前从未使用过 JSP 和 Servlet :()

4

1 回答 1

0

I managed to finish the server to my needs. I used this tutorial for uploads; and this tutorial for downloads. Based on what I learned from the two I managed to create a class for deleting data files from the server:

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);
}
}

Next is my upload.jsp file:

<%@ 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>

And finally my web.xml (that might have some leftover code from other server attempts, but it works fine):

<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>

Hope this helps someone.

于 2012-12-14T11:35:30.673 回答