Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Receive file in Servlet

Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a Servlet which will get a zip file from the client. Servlet is expected to write the file to the filesystem.  

I am using Postman to simulate the request (attached)

Below is my Servlet code

This gives me the below error

What would be the correct way to receive file from the client and save it on the server?

Thank you
[Thumbnail for UploadSevletRequest.PNG]
Master Rancher
Posts: 4666
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files are uploaded using an enc-type of multipart/form-data.
In your servlet you will need something that lets you read in that sort of data.
Simply reading the body won't work.

Apache has the FileUpload package, for example.
Saloon Keeper
Posts: 6594
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly recommend to use an existing library for this, like Apache Commons FileUpload. Much better to start with working code than to code all that yourself.
Hot dog! An advertiser loves us THIS much:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic