0

I'm creating a project of a school. I want to show the uploaded stuff by teachers to students.

But I also need to save the file in the folder which is named as faculty name. Student will be able to browse the main directory and after that he can go in the particular faculties folder.

How can I do it? Any suggestions will be appreciated.

Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

1

For file upload I would start with example like in this answer. Moving files from temporary folder could be easily done by the file uploading action.

For browsing files in your case I would create an action that is able to navigate to the folder where the files are and get a list of files from that folder. Something like this

String file = application.getRealPath("/upload");
File f = new File(file);
String [] fileNames = f.list();
File [] fileObjects= f.listFiles();
for (int i = 0; i < fileObjects.length; i++) {
if(!fileObjects[i].isDirectory()){
    String fname = file+fileNames[i];
    out.println(fileNames[i]);
 }
}

Then map this files to the JSP as links. When that link is clicked you can retrieve the actual path on the server when action is executed. What to do with the data, of course you can return stream result from the action that is used for streaming to the client browser. You can use docs examples from the Struts site or like in this example.

To navigate to the folder use parameters in GET request, that will be used to store current directory in session. You can change it if a user change the current directory from the view layer.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I'm able to upload the file in already created folder. How can I automatic the directory creation in struts with name stored in session? And the code that you've posted, can I be able to view folder like icon means like what we saw in ftp drives opened using browsers? – Manoj Kumar Giri Apr 25 '15 at 09:47
  • Create JSP and icons if you need it to look like ftp drives. – Roman C Apr 25 '15 at 09:51
  • I'm sorry, but how can I display the list of files or folders in the jsp? Do I have to use List to get the List of files and display it using Iterator? or Something else.. – Manoj Kumar Giri Apr 25 '15 at 10:31
  • It's up to you, what did you try? – Roman C Apr 25 '15 at 10:34
  • i'm still confused what to do. The 2nd link above is setting the result with annotations. I don't know how to work with Annotations. And directly writing it giving errors. And also Do I have to add the filenames to the list or the String fname? And how to show it using iterator? – Manoj Kumar Giri Apr 25 '15 at 11:18
  • These are already different questions that have answers [here](http://stackoverflow.com/a/23030719/573032) and [there](http://stackoverflow.com/a/22872041/573032). – Roman C Apr 25 '15 at 11:36
  • Thanks for helping me. Your suggestions lead me to successfully completing this module. – Manoj Kumar Giri Apr 25 '15 at 21:30
  • Because you are new to SO you should read [How it works](http://stackoverflow.com/tour). Also you should upvote all questions and answers that helped you, including those linked to your question. – Roman C Apr 26 '15 at 08:03
  • I tried upvoting the answer, but it says that I don't have enough points. :) But now I'm having 15 , so I'm upvoting. – Manoj Kumar Giri Apr 27 '15 at 04:19
  • You should edit your question and add a code if you don't want it being closed. – Roman C Apr 27 '15 at 19:54