java - 如何在tomcat服务器端访问文档
问题描述
在我的项目中,我有一个 .pdf、.docx 等。我的 tomcat 服务器端的文档。我想访问它们,并且想将它们复制到我的本地计算机驱动器中。但我每次都会收到这个错误。
java.io.FileNotFoundException: \\ptrisf02\group2\Engine_Follow\V2500-A5\V2500-A5_e-Archive\EV11929-02\MiniPack\GEN\EV11929-02 ACCESSORY AND EBU CHECK LIST EEV. 01.pdf (Access is denied)
java.io.FileInputStream.open0(Native Method)
java.io.FileInputStream.open(Unknown Source)
java.io.FileInputStream.<init>(Unknown Source)
java.io.FileInputStream.<init>(Unknown Source)
org.solr.openDocumentServlet.doPost(openDocumentServlet.java:78)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
我试图更改catalina.policy
以授予权限。但它没有用。我怎么解决这个问题??这是我添加到的代码片段catalina.policy
。
//These permissions apply to the E-Archive
grant codeBase "file:${catalina.home}/webapps/E-Archive"{
permission java.io.FilePermission
"${\\\\ptrisf02\\group2\\Engine_Follow}","read";
};
我的代码是这样的
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Docpath=request.getParameter("document");
String docname=request.getParameter("docName");
PrintWriter pw=new PrintWriter(System.out);
pw.write(Docpath);
String r_Docpath=Docpath.replace("\"", "\\");
String r2_Docpath=r_Docpath.replace("\\", "\\\\");
//response.getWriter().append(r2_Docpath);
File original=new File(r2_Docpath);
File destination=new File("D:\\");
try {
//Files.copy(original.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
FileUtils.copyFileToDirectory(original, destination);
}catch(IOException e){
e.printStackTrace();
}
File file=new File("D:\\"+docname);
Path filePath=Paths.get("D:\\"+docname);
response.setContentType(Files.probeContentType(filePath));
response.setHeader("Content-Disposition", "inline;filename="+docname);
InputStream input=new FileInputStream(Docpath);
//FileOutputStream out=new FileOutputStream(Docpath);
ServletOutputStream outs = response.getOutputStream();
BufferedInputStream bis=new BufferedInputStream(input);
BufferedOutputStream bos=new BufferedOutputStream(outs);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = bis.read(buf)) > 0) {
bos.write(buf, 0, bytesRead);
}
bis.close();
bos.close();
//doGet(request, response);
}
解决方案
推荐阅读
- node.js - 安装嵌套依赖的 peerDependencies
- javascript - 尝试通过 NodeJS Lambda 将 XML 站点地图放入 AWS S3 存储桶
- c++ - 如何选择调用的重载函数
- javafx - 在 JavaFx BarChart 中显示垂直线
- javascript - JS中的html类替换不起作用
- ionic-framework - 当我在我的 ionic 应用程序上运行“ionic cordova emulate ios”时架构 x86_64 的未定义符号
- python - 获取属性的所有者实例
- google-api - Google Cloud 启动页面和耗尽自动扩缩器组
- javascript - AngularJS - 数据模型不绑定在下拉列表中
- java - ECJ 因 IllegalArgumentException 处理 log4j 注释而失败