jsp - 如何使任何人都可以访问 JSP 上传的文件?
问题描述
我正在创建一个基于 JSP/servlet 的 Web 应用程序,它将文件上传到服务器上的文件夹。
但是当我点击上传文件的链接时,我无法访问它。
例如,当我尝试访问该文件时,
我已经上传了该文件, 但它不起作用。/Projects/MyFiles/file.html
localhost:8083/Projects/MyFiles/file.html
有人可以帮忙吗?
这是我的代码
@WebServlet("/uploadIDCM")
public class Servlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String finalString = "Yet to";
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File("/Projects/MyFiles/" + File.separator + name));
}
}
finalString = "Success";
request.setAttribute("msg", "File Uploaded Successfully");
} catch (Exception ex) {
finalString = "Exception" + ex.toString();
request.setAttribute("msg", "File Upload Failed due to " + ex);
}
}else{
finalString = "No file found";
request.setAttribute("msg","No File found");
}
}
}
index.js 文件是
<%@ 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>IDCM Builds</title>
</head>
<body>
<Center>
<P>Upload the iOS or Android file(s) here. Upload one by one, or all at once of a specific type</P>
<P>For .html and plist files, destination folder is determined by the File Type option you use below</P>
<P><B>Note:</B> .html, .plist, .ipa or .apk extensions only accepted as valid files</P>
<form action="uploadIDCM" method="post" enctype="multipart/form-data">
<table border=1><tr><Td><label for="type">File(s) Type :</label></td>
<td><select name="ftype">
<option value="iOS_Build">iOS_Build</option>
<option value="Android_Build">Android_Build</option>
</select></td></tr>
<tr><Td><label for="file">Upload File(s):</label></Td>
<Td><input type="file" name="file[]" multiple ></Td></tr>
</table>
<input name="submit" type="submit" value="Upload" />
</Center>
</form>
</body>
</html>
解决方案
推荐阅读
- javascript - Internet Explorer execCommand粗体未重置
- dictionary - ionic:地图导航在 iOS 15 中不起作用
- pytorch - 如何逐个图像地控制(或至少记录)传递给torchvision变换的参数?
- asp.net-core - 使用 Azure ADB2C 进行集成测试
- c# - 您可以使用 MediaToolkit 获得视频缩略图而不读取/写入磁盘吗?
- python - 异常触发后进程返回结果
- sql - 应用程序客户端找不到 SQL 连接
- javascript - 计算价格取决于没有按钮的选择
- mongodb - Mongo模板不返回所有记录
- node.js - 如果从 Flutter 应用程序发送 Passport localStrategy,则没有请求用户