Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Upload file using single jsp code.

Posted by Pankil Patel on August 30, 2010

<!– upload.jsp –>
<%@ page import=”java.io.*”%>
<%
String bookId = “7”;
String param = request.getParameter(“eBookID”);
System.out.println(“Parameter :: ” + param);
if (param != null) {
bookId = param;
}

String contentType = request.getContentType();
System.out.println(“Content type is :: ” + contentType);
if ((contentType != null)
&& (contentType.indexOf(“multipart/form-data”) >= 0)) {
DataInputStream in = new DataInputStream(request
.getInputStream());

int formDataLength = request.getContentLength();

byte 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);

String 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;

File f = new File(“/opt/workspace/eBookStore/ebooks/” + bookId
+ “/assets/voiceprofiles/images/” + saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
System.out.println(“File :: ” + f.getAbsolutePath());
//fileOut.write(dataBytes);
int len = endPos – startPos;
//if (len > dataBytes.length) {
len = dataBytes.length;
//}
System.out.println(dataBytes.length + ” # ” + startPos + ” # ”
+ endPos + ” @@@ ” + len);

fileOut.write(dataBytes, startPos, len – startPos);

fileOut.flush();
fileOut.close();

System.out.println(“File saved as ” + f.getAbsolutePath());

out.println(“File saved as ” + saveFile);

} else {
System.out.println(“sdfsdf “);
}
%>

<html>
<head>
<title>Upload page</title>
</head>
<body>
<form method=”post” ENCTYPE=’multipart/form-data’>

<input type=”file” name=”uploadfile”>

<input type=”submit” name=”Submit” value=”Submit”>
<input type=”reset” name=”Reset” value=”Reset”>
</form>
</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: