Skip to content

Some good fix and changes in UploadServlet #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/info/sudr/file/UploadServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
Expand All @@ -25,7 +27,9 @@ public class UploadServlet extends HttpServlet {

@Override
public void init(ServletConfig config) {
fileUploadPath = new File(config.getInitParameter("upload_path"));
//
// I fixed the uploadpath with the right realPath
fileUploadPath = new File(config.getServletContext().getRealPath(config.getInitParameter("upload_path")));
}

/**
Expand Down Expand Up @@ -93,8 +97,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
}
} // TODO: check and report success
} else {
PrintWriter writer = response.getWriter();
writer.write("call POST with multipart form data");
//
// return the list of all files in the upload folder
PrintWriter writer = response.getWriter();
response.setContentType("application/json");
JSONArray json = new JSONArray();

try {
//
// loop all files in the folder
File[] fileList = fileUploadPath.listFiles();
if (fileList != null) {
for (File f : fileList) {
if (f.isFile()) {
JSONObject jsono = new JSONObject();
jsono.put("name", f.getName());
jsono.put("size", f.length());
jsono.put("url", "upload?getfile=" + f.getName());
jsono.put("thumbnail_url", "upload?getthumb=" + f.getName());
jsono.put("delete_url", "upload?delfile=" + f.getName());
jsono.put("delete_type", "GET");
json.put(jsono);

}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
writer.write(json.toString());
writer.close();
}
}
}

Expand All @@ -115,7 +148,14 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.setContentType("application/json");
JSONArray json = new JSONArray();
try {
//
// this list is for file items
List<FileItem> items = uploadHandler.parseRequest(request);
//
// this map will contains other parameters from form input fields or formData option
// see documentation : https://github.com/blueimp/jQuery-File-Upload/wiki/How-to-submit-additional-Form-Data
Map<String, String> otherParameters = new HashMap<String, String>();
//
for (FileItem item : items) {
if (!item.isFormField()) {
File file = new File(fileUploadPath, item.getName());
Expand All @@ -129,6 +169,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
jsono.put("delete_type", "GET");
json.put(jsono);
}
else {
//
// these are the other form fields or elements of formData option
otherParameters.put(item.getFieldName(), item.getString());

}
}
} catch (FileUploadException e) {
throw new RuntimeException(e);
Expand Down