简体   繁体   English

尝试写入文件时,Tomcat权限被拒绝

[英]Tomcat permission denied when trying to write a file

I am trying to send a zip via tomcat server. 我想通过tomcat服务器发送一个zip。 But when I try to access http://localhost:8094/fetna-project-rest/webapi/retdoc/10 I got a permission error which I included in the bottom. 但是当我尝试访问http:// localhost:8094 / fetna-project-rest / webapi / retdoc / 10时,我收到了一个权限错误,我将其包含在底部。 I run the server on ubuntu. 我在ubuntu上运行服务器。 Can you please help me? 你能帮我么? My source codes are here: 我的源代码在这里:

public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {

    System.out.println("Writing '" + fileName + "' to zip file");

    File file = new File(fileName);
    if (!file.exists()) {
        file.createNewFile();
    }

    FileInputStream fis;
    fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    zos.closeEntry();
    fis.close();
}

@GET
@Path("retdoc/{id}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response retrieveDocument(@PathParam("id") String ID) throws IOException {

    FileOutputStream fos = new FileOutputStream("test.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);

    String file1Name = "test.txt";

    addToZipFile(file1Name, zos);
    zos.close();
    fos.close();
    File zippedFile = new File("test.zip");
    return Response.ok(FileUtils.readFileToByteArray(zippedFile))
            .type("application/zip")
            .header("Content-Disposition", "attachment; filename=\"retrievedDocuments.zip\"")
            .build();
}

and my stacktrace: 和我的堆栈跟踪:

message org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: test.zip (Permission denied)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: test.zip (Permission denied)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:487)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: test.zip (Permission denied)
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:278)
    org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:260)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:509)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:334)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.io.FileNotFoundException: test.zip (Permission denied)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    java.io.FileOutputStream.<init>(FileOutputStream.java:110)
    org.eu.srdc.ehealth.MyResource.retrieveDocument(MyResource.java:78)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.30 logs.

The user running tomcat doesn't have permission to write the file, just as the error says it. 运行tomcat的用户没有写入文件的权限,就像错误所说的那样。 Would suggest you choose a specific folder where your uploaded files will be saved to and give the tomcat user write permissions to it (change username and path accordingly): 建议您选择保存上传文件的特定文件夹,并为其授予tomcat用户写入权限(相应地更改用户名和路径):

$ sudo mkdir /home/tomcat/uploads
$ sudo chown tomcat:tomcat /home/tomcat/uploads
$ sudo chmod u+wrx /home/tomcat/uploads

Then change your servlet code to use the upload path (modified line below): 然后更改您的servlet代码以使用上传路径(下面的修改行):

FileOutputStream fos = new FileOutputStream("/home/tomcat/uploads/test.zip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝) - FileNotFoundException (permission denied) when trying to write file to sdcard in Android 尝试在Java中使用FileWriter / BufferedWriter进行写入时,权限被拒绝。 - Permission denied when trying to write with FileWriter/BufferedWriter in java. 在 Java/Springboot 中创建文件时权限被拒绝错误 - Tomcat - Permission Denied Error when Creating file in Java/Springboot - Tomcat 将图像文件写入 sdcard 时出现“Permission Denied”错误 - “Permission Denied” error when write image file into sdcard 蚀| 尝试在Ubuntu下打开sh文件时权限被拒绝 - Eclipse | Permission denied when trying to open sh file under Ubuntu 尝试写入新文件时,FileNotFoundException(拒绝访问) - FileNotFoundException (Access is denied) when trying to write to a new file 嵌入的tomcat权限被拒绝 - Permission denied with embeded tomcat Android 6.0文件写入权限被拒绝 - Android 6.0 File write permission denied 使用 java 写入文件的权限被拒绝 - Permission denied to write file using java 尝试将字符串从Web写入文件时出现FileNotFoundException(permission) - FileNotFoundException(permission) when trying to write string from web to file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM