• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

image upload to a paticular folder at web server

 
donghai fu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please please please help me, I am stuck on this for quiet a while and checked the previous posts here, but still could not fix this. My code is as below, when run it, it always shows failure, and nothing uploaded on the server.

1) I don't know which line to set up the folder on the server for me to save the images, it is: "/upload/"

2) I searched the online posts, says I need to install the "The Commons FileUpload package" through http://commons.apache.org/fileupload/index.html, I don't know how to install the page, in which folder of my server?

Really appreciate your help.


-----------------------------------------------------------
index.jsp:

<form method="post" ACTION="upload.jsp" name="uploadForm" ENCTYPE='multipart/form-data'>
<input type="file" name="uploadFile" >
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</form>
-----------------------------------------------------------

My "upload.jsp" is as:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " + contentType);
String imageSave=null;
byte dataBytes[]=null;
String saveFile=null;
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
dataBytes = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
// out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
// out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
try
{
FileOutputStream fileOut = new FileOutputStream("c://"+saveFile);
// fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
imageSave="Success";
}catch (Exception e)
{
System.err.println ("Error writing to file");
imageSave="Failure";
}
}
%>
<html>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>File Upload Success/Failure</TITLE>
</HEAD>
<body>
<table align="center" width="300" height="100">
<tbody>
<tr>
<th width="85">Image Name:</th>
<td width="60"><%=saveFile%></td>
</tr>
<tr>
<th width="85">No Of Bytes:</th>
<td width="60"><%=dataBytes.length%></td>
</tr>
<tr>
<th width="85">Image Save:</th>
<td width="60"><%=imageSave%></td>
</tr>
</tbody>
</table>
</body>
</html>

-----------------------------------------------------------
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: If you've looked at other topics on this issue, you'll have noted that the Java code should not be in a JSP but in a servlet. So first step is to ditch the JSP and put the file upload handling code in a servlet.

Step 1: Place the jar file for the commons library in the web app's WEB-INF/lib folder.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic