0

I made an application which received 2 parts: a picture and some text. The start of the code is:

  @WebServlet("/App")
@MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
        maxFileSize=1024*1024*50,       // 50 MB
        maxRequestSize=1024*1024*100)       // 100 MB
public class App extends HttpServlet {

    private static final long serialVersionUID = 205242440643911308L;
    private static final String PREFIX = "stream2file";
    private static final String SUFFIX = ".tmp";

    private static final String UPLOAD_DIR = "uploads";

    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp) throws ServletException, IOException {

        JsonObject servletResponse = new JsonObject();

        System.out.println("------------------------------");
        System.out.println(req.getContentType());
        System.out.println("------------------------------");
        String text = null;
        String uuid = UUID.randomUUID().toString();
        Properties props = new Properties();
        File dbPropsFile = new File("/config.properties");
        FileReader fileReader = new FileReader(dbPropsFile);
        props.load(fileReader);

        for (Part part : req.getParts()) {
            if(getFileType(part).equals("image")) {

The code fails on

for (Part part : req.getParts()) {

It works completely fine if I'm running it with mvn jetty:run, but when deployed to the server, it just crashes. Please help!!!

When trying to do System.out.println(req.getContentType()); this comes as null on the Tomcat and on the Jetty server, but it comes with the expected value on the mvn jetty:run

1 Answers1

0

If the HttpServletRequest.getContentType() is null, then your request isn't satisfying the requirements for @MultipartConfig which requires the Content-Type value to be multipart/form-data

Ensure that your form is being submitted properly on the network first.

Perhaps add some unit tests to your project to ensure that the servlet is behaving as expected, allowing you to focus your attention on the form submit / request side.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136