gpt4 book ai didi

JavaEE、Servlet 创建指向文件夹中文件的链接

转载 作者:行者123 更新时间:2023-11-28 23:15:08 27 4
gpt4 key购买 nike

有一个servlet 接受来自客户端的文件并将它们存储在一个文件夹中。现在需要列出此文件夹中的文件并创建指向它们的链接(即,单击文件名并从您那里下载)。

现在只输出文本形式的文件列表。如何创建到它们的链接?我读到为此公开标题就足够了,但是这是如何完成的并且尚未找到。示例代码:

public class FileListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public FileListServlet() {
super();
}

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {


String param = "/dirs";

PrintWriter w = res.getWriter();
res.setContentType("text/html");

String root="/dirs";
java.io.File dir = new java.io.File(root);

File[] fileList = dir.listFiles();

w.println("<H2><FONT COLOR=TEAL>" + "Total number of files in the choosen directory - " +
fileList.length + "</FONT></H2>");
w.println("<H3><FONT COLOR=PURPLE>" +
"Directory path - " + param + "</FONT></H3><HR>");
w.println("<TABLE BORDER=0 CELLSPACING=5>");

for(int i = 0; i < fileList.length; i++)
printName(fileList[i], w);

w.println("</TABLE><HR>");

}

private void printName(File name, PrintWriter output)
{

String type = name.isDirectory()
? " (Directory)" : " (File)";
output.println("<TR><TD>" + type + "</TD><TD><FONT COLOR=BLUE>"
+ name.getName() + "</FONT></TD></TR>");

}

public String getServletInfo()
{
return "This servlet shows a content of a directory" +
"mentioned in dirToShow parameter or property.";
}
}

enter image description here

最佳答案

我解决了我的问题。以防有人需要它或有人知道更漂亮的方法。

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public FileListServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter printWriter = response.getWriter();
response.setContentType("text/html");

File currentFolder = new File(".");
File workingFolder = new File(currentFolder, "Sorted files");

String root = workingFolder.getAbsolutePath();
java.io.File dir = new java.io.File(root);

File[] fileList = dir.listFiles();

printWriter.println("<H2><FONT COLOR=TEAL>" + "Total number of files in the choosen directory - " +
fileList.length + "</FONT></H2>");
printWriter.println("<H3><FONT COLOR=PURPLE>" +
"Directory path - " + root + "</FONT></H3><HR>");
printWriter.println("<TABLE BORDER=0 CELLSPACING=5>");



for(int i = 0; i < fileList.length; i++) {
printName(fileList[i], printWriter);
}
printWriter.println("</TABLE><HR>");

}

private void printName(File file, PrintWriter output)
{

System.out.println(file.getName());

output.println("<tr><td><a href=\"https://Upload/DownloadServlet?name="
+file.getName()+"\">" + file.getName() + "</a></td></tr>" );

}

public String getServletInfo()
{
return "This servlet shows a content of a directory" +
"mentioned in dirToShow parameter or property.";
}
}

和DownloadServlet

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class DownloadServlet
*/
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");

String fileType = "multipart/form-data";

response.setContentType(fileType);

response.setHeader("Content-disposition", "attachment; filename=" + name);

File currentFolder = new File(".");
File workingFolder = new File(currentFolder, "Sorted files");

String root = workingFolder.getAbsolutePath();

File file = new File(root + File.separator + name);

OutputStream output = response.getOutputStream();
FileInputStream input = new FileInputStream(file);

byte[] buffer = new byte[4096];
int lenght;

while( ( lenght = input.read(buffer) ) > 0 ) {
output.write(buffer, 0, lenght);
}

input.close();

output.flush();
output.close();

response.getOutputStream().flush();
response.getOutputStream().close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

关于JavaEE、Servlet 创建指向文件夹中文件的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com