3

I'm trying to create a file in my server. I have sent a image, and I want to create that Image in a folder of my server, but with relative path.

String filePath = "C:\\Users\\Administrador\\Desktop\\Proyecto\\clienteServidor\\Server\\folder\\image.jpg";
File imageFile = new File(filePath);
...

I'm doing with the absolute path.

Thanks

Mario Erro
  • 31
  • 1
  • 2
  • 1
    Hi and welcome to SO! Please specify your question - what is the exactly problem you having while implementing this? What does not work as expected? – akluth Apr 15 '13 at 20:46
  • 1
    you can't. depending on the server you use, the file system may not be writable or even available to your app. If you want more details, you need to tell us what server you are using. – Dmitry B. Apr 15 '13 at 20:48
  • That source doesn't have any problem. But I don''t want to use all path. I got Tomcat 7.0 – Mario Erro Apr 15 '13 at 20:54
  • Maybe temp dir will be good `String path = System.getProperty("java.io.tmpdir"); ? – Sergii Zagriichuk Apr 15 '13 at 21:05
  • `java.io.tmpdir` is good for temporary storage but not if you want to store uploaded images permanently. – Cebence Apr 15 '13 at 21:16
  • @MarioErro You must write any file you want to keep to a directory outside the application. Pass the directory path to the app using a system property or property file. – Jim Garrison Apr 15 '13 at 21:23
  • That is the response; C:\Users\ADMINI~1\AppData\Local\Temp\ The temp folder... It's a wrong solution – Mario Erro Apr 15 '13 at 21:34
  • If it is a J2EE webapp, you can use the servlet context's **`getRealPath`** for a path relative to.the webapp dir, – Joop Eggen Oct 04 '17 at 22:05

2 Answers2

0

hard coding a directory is seldom good for coding. What happens if there is a typo in your code. Using a combination of ./ or ./*

or even using

new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

This is explained here.

Akin Okegbile
  • 1,108
  • 19
  • 36
  • With that Path MyClass gives me. /C:/Users/Administrador/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/SetasServer/WEB-INF/classes/com/rest/resource/PhotoResource.class What's this folder? Because I have my project in other folder... – Mario Erro Apr 16 '13 at 07:37
  • 1
    When you run your project inside Eclipse its default behavior is to create a temporary folder like that one and run your webapp from there so it doesn't interfere with an existing Tomcat installation. – Cebence Apr 16 '13 at 08:36
  • I assume you mean the code snippet. What about the combination of the ./ and ./ – Akin Okegbile Apr 16 '13 at 18:31
-1

It is doable but, as Dmitry said, it might not work on every server. SecurityManager class should be consulted if your webapp has the privilege to write to that folder. or you will get an exception.

One way to do it is via ServletContext:

URL webAppRoot = this.getServletConfig().getServletContext()
    .getResource("/images/new-image.jpg");

This will point to your ${tomcat}/webapps/mywebapp/images/new-image.jpg.

Another way is via ProtectionDomain:

URL runningClassLocation = this.getClass().getProtectionDomain()
    .getCodeSource().getLocation();

But this will most likely give you jar:file://...myapp.jar!/my/package/servlet.class.

After you have the URL you convert it to File and append any relative path to your image folder.

UPDATE: I agree with Jim, and emphasize that doing it like this is just for academic purposes. Java is not like PHP so you shouldn't have uploads folder inside your web application's folder. Usually this is done by enabling an administrator-level user to specify a file path to a folder reserved for your application's storage needs.

Cebence
  • 2,406
  • 2
  • 19
  • 20
  • This idea _may_ work in some situations, but in general cannot be relied upon. If the app is running from a war file this will fail. – Jim Garrison Apr 15 '13 at 21:22
  • 1
    @Jim AFAIK server will unpack `mywebapp.war` file into `${tomcat}/work/mywebapp` so it will work. How do you think **Struts** reads its configuration files when specified as `/WEB-INF/struts-config.xml`? With the first approach I mentioned. – Cebence Apr 15 '13 at 21:27
  • I said nothing about READING. You can read all you want, the Java system is designed for that. What you cannot do is _write_ (i.e. create a file), which is what the OP is attempting. – Jim Garrison Apr 15 '13 at 21:29
  • You can read it with `getResource()` but it's a URL from which one can get a `File` and check if it can write to that location. – Cebence Apr 15 '13 at 21:31
  • With your second option that is the answer; /C:/Users/Administrador/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/SetasServer/WEB-INF/classes/com/rest/resource/PhotoResource.class A temp folder. And I don't have a lot of about Servlet, How I could call a servlet method, like for example doPost??? Could you explain me better? Thanks – Mario Erro Apr 15 '13 at 21:35
  • If you are doing this in a JSP page you are already in a servlet so just place that code `<%` HERE `%>`. – Cebence Apr 15 '13 at 21:46
  • It isn't a JSP, it's only a normal server. (Dinamic Web Project) – Mario Erro Apr 16 '13 at 07:31
  • Dynamic Web Project **means** that you are working with `Servlet`s. If your class is not inheriting from `Servlet` then you are doing it wrong. And a Java Server Page (JSP) is just an *upgrade* so you don't have to handle many HTML code inside your servlet (Java code). I suggest you read some Servlet / JSP tutorials to familiarize with the technology. – Cebence Apr 16 '13 at 07:57