gpt4 book ai didi

java - Apache Tomcat - 使文件可从 bin 文件夹下载

转载 作者:行者123 更新时间:2023-11-28 22:53:55 25 4
gpt4 key购买 nike

我正在使用一个在 tomcat bin 文件夹中创建文件的 API,我已经创建了一个 servelet 来使文件可下载,但是我应该提供什么路径来指向 bin 文件夹?网站网址就像

abc.co.uk/test/home.jsp

这是我的 servlet 代码:

private static final int DEFAULT_BUFFER_SIZE = 102400; //100KB
private String filePath;

public void init() throws ServletException
{
this.filePath = getServletContext().getRealPath("/");
System.out.println("Original Path: " + filePath + ": " + this.filePath.indexOf("webapps") + 1);
this.filePath = this.filePath.substring(0, this.filePath.indexOf("webapps")) + "home/";
System.out.println("FilePath1: " + this.filePath);
//"C:\\Users\\Admin\\Documents\\NetBeansProjects\\MSc\\model";
}

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

String requestedFile = request.getPathInfo();

// Check if file is actually supplied to the request URI.
if (requestedFile == null) {
// Do your thing if the file is not supplied to the request URI.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Decode the file name (might contain spaces and on) and prepare file object.
File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));

// Check if file actually exists in filesystem.
if (!file.exists()) {
// Do your thing if the file appears to be non-existing.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Get content type by filename.
String contentType = getServletContext().getMimeType(file.getName());

// If content type is unknown, then set the default value.
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
// To add new content types, add new mime-mapping entry in web.xml.
if (contentType == null) {
contentType = "application/octet-stream";
}

// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
// Gently close streams.
close(output);
close(input);
}



processRequest(request, response);
}

这是 anchor 标记代码:

  <a href="./final.cmp">DOWNLOAD MODEL FILE!!!</a>

我需要一些帮助。

最佳答案

可以通过环境变量CATALINA_BASE或系统属性catalina.base访问tomcat bin目录

例如:

System.getProperty("catalina.base") + File.separator + "bin"

bin 目录中生成文件听起来像是最糟糕的地方之一。您最好将生成的文件放在它们自己的目录中,您可以在其中分离出权限要求。

关于java - Apache Tomcat - 使文件可从 bin 文件夹下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443375/

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