0

I'm working on a mini-project that can upload certain files to a server. I'm using java and mysql. According to things I read the 'right way' is to save the file to a folder in my server and put the file path in mysql.

A Part of my code:

File source = new File(fileChooser.getSelectedFile().getPath());
                    File dest = new File(destChooser.getSelectedFile().getPath());

                    String tempUpdate1 = "Insert into images (ID, file_name, file_path)"
                            + "VALUES ('"+ID+"' , '"+fileChooser.getSelectedFile().getName()+"' , '"+destChooser.getSelectedFile().getPath()+"')";
                    conn = Connect.ConnectDB();
                    Statement stemt2 = conn.createStatement();
                    stemt2.executeUpdate(tempUpdate1);
                    FileUtils.copyFileToDirectory(source, dest);
                    JOptionPane.showMessageDialog(null, "File Uploaded Successfully!");

then i tried running it. It successfully copied the file to my desired folder. The problem is in my mysql table where i save the path of the file. The table's like this:

 |   ID   |   file_name   |      file_path        |
 |   1    |   sample.docx |  C:UsersMEDesktopest  |
 |   2    |   sample.jpg   |  C:UsersMEDesktopest  |

I tried seeing the output of the file path myself using JOptionPane it returned normal with the separators, but using the same variable and puting it in mysql, that's what i get as seen above, no separators.

Am i really doing it right? I did what is instructed on the topics related to this but no one seems to be complaining with the path. I'm wondering if did something wrong somewhere.

I'm planning to access the file using the file path as my project progress. Just would like to ask if the path file is accessible since i dont see any separator such as '//', '/' or '\'

meepawned
  • 15
  • 5

1 Answers1

0

it is almost good.

imagine that you have two files named same way. you can have only one of them on your server. I would save file under coded name like time in milis at moment of saving action.

Your table would contain then original name sample.jpg and file name 49473996034732 for example (in separate column of course). This way you can save as many sample.jpg as you like. Request link should point to file name 49473996034732 so you always will know for which file is request. Then in response, when you set content to proper type you can set user friendly name.

modified table

| ID | file_name | server_file_name | file_path |

| 1 | sample.docx | 49473996034732 | C:UsersMEDesktopest |

| 3 | sample.jpg | 49473996034850| C:UsersMEDesktopest |

| 2 | sample.jpg | 49473996034988 | C:UsersMEDesktopest |

second thing is that i noticed is that you allow user to pick destination folder for containing file. Shouldn't is be fixed location? This way it would be more like server behavior. But im guessing this is just an example code fast codded in Swing

T.G
  • 1,913
  • 1
  • 16
  • 29
  • thanks for answering! and yes, it's just a sample when i let the user choose the destination, im planning on just letting the user choose the file and ill set the fixed location for the destination. I don't get the time in millis and server_file_name column. What is the purpose of that extra one? To prevent the need to replace items when having the same file name? – meepawned Jan 12 '15 at 13:41
  • time in milis is example of source of unique filenames. In real project it would be not enough anyway (server multitasking might result in attempt to save two files in two threads in same time). Reason for this unique name is that you will be allowed to save many files with same name (sample.jpg). in example provided under C:UsersMEDesktopest you will have two files sample.jpg stored in files 49473996034850 and 49473996034988. If some one will request file via www.example.com/get/49473996034850 you will get desired sample.jpg (dont forget to set proper name in HttpServletResponse or similar) – T.G Jan 12 '15 at 14:53
  • ok ok thanks, but 1 more question. is my filepath accessible? i mean if i intend to retrieve something i uploaded in the server for instance the sample.jpg, is that file path valid? without the '/' separators and all? – meepawned Jan 12 '15 at 21:31
  • it depends on the system on which the application server is running. Read more about manipulating file system on server. You can start from reading http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet and http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet. Good luck. – T.G Jan 13 '15 at 12:06