• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image upload

 
guru prasanth
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the ServletsFaq for an entry on file uploading.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!