Skip to content

Commit 0adf607

Browse files
committed
Update src/info/sudr/file/UploadServlet.java
1) init() method:  I fixed a bug (?) in the init() to the fileUploadPath  initialization. In my localhost execution the item.write(file); in doPost method throwed an exception. Now with the correct realPath it's ok. 2) doGet() method In the doGet method I add the feature to list all the uploaded files, already uploaded in the uploadedFolder.
1 parent bcc2bea commit 0adf607

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/info/sudr/file/UploadServlet.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class UploadServlet extends HttpServlet {
2525

2626
@Override
2727
public void init(ServletConfig config) {
28-
fileUploadPath = new File(config.getInitParameter("upload_path"));
28+
//
29+
// I fixed the uploadpath with the right realPath
30+
fileUploadPath = new File(config.getServletContext().getRealPath(config.getInitParameter("upload_path")));
2931
}
3032

3133
/**
@@ -93,8 +95,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
9395
}
9496
} // TODO: check and report success
9597
} else {
96-
PrintWriter writer = response.getWriter();
97-
writer.write("call POST with multipart form data");
98+
//
99+
// return the list of all files in the upload folder
100+
PrintWriter writer = response.getWriter();
101+
response.setContentType("application/json");
102+
JSONArray json = new JSONArray();
103+
104+
try {
105+
//
106+
// loop all files in the folder
107+
File[] fileList = fileUploadPath.listFiles();
108+
if (fileList != null) {
109+
for (File f : fileList) {
110+
if (f.isFile()) {
111+
JSONObject jsono = new JSONObject();
112+
jsono.put("name", f.getName());
113+
jsono.put("size", f.length());
114+
jsono.put("url", "upload?getfile=" + f.getName());
115+
jsono.put("thumbnail_url", "upload?getthumb=" + f.getName());
116+
jsono.put("delete_url", "upload?delfile=" + f.getName());
117+
jsono.put("delete_type", "GET");
118+
json.put(jsono);
119+
120+
}
121+
}
122+
}
123+
} catch (Exception e) {
124+
throw new RuntimeException(e);
125+
} finally {
126+
writer.write(json.toString());
127+
writer.close();
128+
}
98129
}
99130
}
100131

0 commit comments

Comments
 (0)