below file uploading code has one error "toBytes[i-start]=fromBytes; " incompatable type error please help me how to solve this error <%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %> <%@ page import="java.io.FileWriter,java.io.IOException" %> <% String savePath = "",filepath="",filename=""; String contentType ="", fileData = "", strLocalFileName = ""; int startPos = 0; int endPos = 0; %> <%! void copyByte(byte[] fromBytes,byte[] toBytes,int start,int len) { for(int i=start;i<(start+len);i++) { toBytes[i-start]=fromBytes; } } %> <% int BOF=0,EOF=0; contentType = request.getContentType(); out.println("Content type is :: " +contentType); if ((contentType != null) && (contentType.indexOf("multipart/form-data") >=0)) { DataInputStream in = new DataInputStream(request.getInputStream()); DataInputStream in1 = in; int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while (totalBytesRead < formDataLength) { byteRead = in1.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } out.println("totalBytesRead:"+totalBytesRead +":formDataLength = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead < 3) { return; //exit if file length is not sufficiently large } String boundary = ""; String s = ""; int count = 0; int pos = 0; //loop for extracting boundry of file //could also be extracted from request.getContentType() do { copyByte(dataBytes, line, count ,1); //read 1 byte at a time count+=1; s = new String(line, 0, 1); fileData = fileData + s; pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name if(pos != -1) endPos = pos; }while(pos == -1); boundary = fileData.substring(startPos, endPos); //loop for extracting filename startPos = endPos; do { copyByte(dataBytes, line, count ,1); //read 1 byte at a time count+=1; s = new String(line, 0, 1); fileData = fileData + s; pos = fileData.indexOf("filename=\"", startPos); //set the file name if(pos != -1) startPos = pos; }while(pos == -1); do { copyByte(dataBytes, line, count ,1); //read 1 byte at a time count+=1; s = new String(line, 0, 1); fileData = fileData + s; pos = fileData.indexOf("Content-Type: ", startPos); if(pos != -1) endPos = pos; }while(pos == -1); filename = fileData.substring(startPos + 10, endPos - 3); //to eliminate "from start & end strLocalFileName = filename; int index = filename.lastIndexOf("\"); if(index != -1) filename = filename.substring(index + 1); else filename = filename; //loop for extracting ContentType boolean blnNewlnFlag = false; startPos = endPos; //added length of "Content-Type: " do { copyByte(dataBytes, line, count ,1); //read 1 byte at a time count+=1; s = new String(line, 0, 1); fileData = fileData + s; pos = fileData.indexOf("\n", startPos); if(pos != -1) { if(blnNewlnFlag == true) endPos = pos; else { blnNewlnFlag = true; pos = -1; } } }while(pos == -1); contentType = fileData.substring(startPos + 14, endPos); //loop for extracting actual file data (any type of file) startPos = count + 1; do { copyByte(dataBytes, line, count ,1); //read 1 byte at a time count+=1; s = new String(line, 0, 1); fileData = fileData + s; pos = fileData.indexOf(boundary, startPos); //check for end of file datai.e boundry value }while(pos == -1); endPos = count - boundary.length(); //file data extracted out.println("0. Local File Name = " + strLocalFileName); out.println("1. filename = " + filename); out.println("2. contentType = " + contentType); out.println("3. startPos = " + startPos); out.println("4. endPos = " + endPos); out.println("5. boundary = " + boundary); //create destination path & save file there String appPath = application.getRealPath("/"); out.println("appPath : " + appPath); String destFolder = appPath + "images/banner/"; filename= destFolder + filename; FileOutputStream fileOut = new FileOutputStream(filename); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); out.println("File saved as >> " + filename); //file saved at destination //out.println("File data : ******"+(new String(dataBytes,startPos, (endPos - startPos))) +""); } else { out.println("Error in uploading "); } %>
Hi Friend,
Try the following code:
1)page.jsp:
<%@ page language="java" %> <HTml> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION= "uploadandstore.jsp" METHOD=POST> <br><br><br> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center> <td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Send File"> </td> </tr> <table> </center> </FORM> </BODY> </HTML>
2)uploadandstore.jsp:
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <% String saveFile=""; String contentType = request.getContentType(); 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); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); 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; saveFile="C:/"+saveFile; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br><table border="2"><tr><td><b>You have successfully upload the file:</b> <% out.println(saveFile);%></td></tr></table> <% } %>
Thanks