Tomcat autorisation refusée lors de la tentative d'écrire un fichier
J'essaie d'envoyer un fichier zip via le serveur tomcat. Mais lorsque j'essaie d'accéder à http://localhost:8094/fetna-project-rest/webapi/retdoc/10 j'ai eu une erreur de permission qui j'ai mis dans le fond. Je lance le serveur sur ubuntu. Pouvez-vous svp m'aider? Mes codes sources sont ici:
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();
}
et mon 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.
OriginalL'auteur Arda Güney | 2016-02-16
Vous devez vous connecter pour publier un commentaire.
L'utilisateur qui exécute le serveur tomcat n'a pas l'autorisation d'écrire le fichier, tout comme l'erreur, dit-il.
Vous suggérons de choisir un dossier où les fichiers téléchargés seront enregistrés et donner à l'utilisateur tomcat autorisations d'écriture (modifier le nom d'utilisateur et le chemin d'accès en conséquence):
Puis changer votre servlet code pour utiliser le chemin de téléchargement (modifié la ligne ci-dessous):
Non, il n'y a pas de "défaut" dans ce cas, tout dépend de la situation sur le fichier que vous spécifiez (
fileName
?).Je l'ai trouvé dans un Tomcat, le fichier de configuration (ou peut-être lancer le script) :D. Pour moi a été
/var/lib/tomcat8
- par ailleurs, ce dossier appartenait à root. Merci quand même, @ernest_kOriginalL'auteur ernest_k