0

I am trying to write a Java code in my Spring MVC Web Application that will create a file and save the file to a local directory. My code is as follows:

String fileName = new SimpleDateFormat("yyyyMMddHHmmss'.csv'").format(new Date());
File file = new File("C:\\my-files\\"+fileName);
FileWriter writer = new FileWriter(file);
for (Obj obj : objList) 
{
    StringBuilder sb = new StringBuilder();
    sb.append(DATA);
    writer.write(sb.toString());
    writer.flush();
    writer.write("\r\n");
}
writer.close();

This code works for me when I run locally. But when I try to run this code from the server, the file is not getting created. I am not sure if I should be setting some permissions for writing file when the code is run from a server.

I want the file to be created and downloaded to the local drive of the computer from which the web application is accessed. I dont want the file to be saved anywhere else

Geo Thomas
  • 1,139
  • 3
  • 26
  • 59
  • is there any exception? what did you get while debugging? – Shubham Feb 27 '19 at 10:18
  • Have you taken a look at the directory *C:\my-files* on your server? Maybe the file is there... – deHaar Feb 27 '19 at 10:19
  • maybe server don't have C:\\ drive – Akash Shah Feb 27 '19 at 10:19
  • @deHaar, I want to write to a local drive in the computer from which the application is accessed. – Geo Thomas Feb 27 '19 at 10:20
  • @GeoThomas that makes no sense. basically, if anyone uses your (remote) application, you would be able to start changing things on their workstation? – Stultuske Feb 27 '19 at 10:22
  • @GeoThomas The application will create the file **on the server where the application is executed** because it *thinks locally*. The path will be interpreted as a local one. – deHaar Feb 27 '19 at 10:22
  • Its not usually recommended to write files on C drive on windows as some people might have read-only access to C drive. try writing on other disks where your program have permission to write – Mustahsan Feb 27 '19 at 10:22
  • @Mustahsan, I am ok with any drive on the computer. it is just a data file, a csv. Just trying to save it to local machine – Geo Thomas Feb 27 '19 at 10:24
  • if possible store file in resource folder – Akash Shah Feb 27 '19 at 10:24
  • @deHaar, is there any option to save to the computer rater than the server – Geo Thomas Feb 27 '19 at 10:24
  • 1
    Maybe create the file on the server an make it be downloaded afterwards to have it on a remote machine. – deHaar Feb 27 '19 at 10:24
  • @GeoThomas you can easily write data on any device the program is running until your program has permissions to write. in this case that computer might not even have C drive? – Mustahsan Feb 27 '19 at 10:27
  • @Mustahsan they are not talking about a machine on which the program is running. for instance: I search google.com in my browser, the goal is to have google create a file on my machine. But I don't locally run google, just my browser – Stultuske Feb 27 '19 at 10:28
  • i think he has a local program/ desktop app which will run locally? @GeoThomas – Mustahsan Feb 27 '19 at 10:29
  • @Mustahsan that's what he said: as long as he runs it locally, it works. but he doesn't want to run it locally – Stultuske Feb 27 '19 at 10:30
  • that makes no sense, only option he will have to download file on user's machine using octetStream rest response – Mustahsan Feb 27 '19 at 10:34
  • one solution is stored in the server then using the help of browser store into a local machine . – Akash Shah Feb 27 '19 at 10:36
  • I would delete your question as it is **obviously impossible** for a web server to write files to a client machine. Imagine the security implications. You can generate the file on the server and stream as the response to a web request and I'm sure you can find plenty of examples of how do do that. – Alan Hay Feb 27 '19 at 12:22
  • @ deHaar, is it possible to create my file and then download the file to the client machine? Something like https://www.baeldung.com/servlet-download-file – Geo Thomas Feb 28 '19 at 07:01
  • i have a question here, is your server in the same windows machine or different? the code you have written will only work on local machine where your code is being executed if any other machine it is impossible to do so without making any ftp or sftp connection from client to server this is because of windows security. – Shubham Mar 01 '19 at 08:52
  • @ Shubham, server is not in the same windows machine – Geo Thomas Mar 01 '19 at 10:03

1 Answers1

0

You have to check if the servers harddrive is also called C:\ and if the directory my-files exists...

I would generally recommend using relative paths

File file = new File(fileName);

Or create your own directory and also use relative paths

File file = new File("mydir");
if(!file.exists()) {
  file.mkdir();
}

file = new File(fileName);
... rest of your code
XRP DEV
  • 32
  • 3
  • 1
    OK... And how does this solve the question? The file still won't be created on the client machine – Stultuske Feb 27 '19 at 10:31
  • The question is not about the code itself, it is about a running the code on a maching and creating the file on another one, which will not work the way tried here. – deHaar Feb 27 '19 at 10:33
  • @N. Helldorff, how do i initiate a download if I create the file just with filename and the write data to it – Geo Thomas Feb 28 '19 at 08:58
  • How does somebody initiate a download?? I would recommend you to write a REST Controller https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers – XRP DEV Feb 28 '19 at 10:13