• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image upload

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Iam trying to upload jpeg image.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Insert title here</title>

</head>
<body>
<form action="http://localhost:8080/app/action" method="POST" enctype="application/x-www-form-urlencoded">
Image <input type="file" name="test" length=10/>

<input type="submit" />
</form>

</body>
</html>

In servlet the below code

DataInputStream din = new DataInputStream(request.getInputStream());



byte[] data = new byte[0];
byte[] buffer = new byte[50];
int bytesRead;
while ((bytesRead = din.read(buffer)) > 0 ) {

byte[] nData = new byte[data.length + bytesRead];
System.arraycopy(data, 0, nData, 0, data.length);
System.arraycopy(buffer, 0, nData, data.length, bytesRead);
data = newData;
}

What i see is servletinput stream does't have any values.It does't even enter while loop.
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first place to look at is the client-side. Your form will submit the name of the image file, not the contents. You should be using enctype="multipart/form-data" rather than enctype="application/x-www-form-urlencoded". It would helpful to examine the data going over the wire to actually see what the your HTTP request looks like. Eclipse has TCP/IP Monitor built-in for this; WireShark is a good external application.

Also, length is not an attribute of input - you probably want to use size and/or maxlength.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the ServletsFaq for an entry on file uploading.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic