Reading a book (JSP Application and Development Technology) Using JSP file upload, write a real speechless, there are a lot of problems at all, after uploading a kb file, only KB is left, the encoding must be GBK or GB2312. Otherwise, all the uploaded files are intercepted incorrectly.
After a long time, I found the problem
Int startPos = (file. substring (0, pos). getBytes (). length;
// The location where the file data is obtained.
Int endPos = (file. substring (0, boundaryLocation). getBytes (). length;
Here, the problem is that the number of bytes obtained based on the string and then truncated from the byte array is not correct. Then, we implemented it by ourselves. Instead of using string bytes, we found bytes by ourselves. As follows:
UploadTelRecord. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. io. dataInputStream "%> <% @ page import =" java. io. fileOutputStream "%> <% @ page import =" java. io. file "%> <% @ page import =" java.net. URLEncoder "%> <! -- Upload a call recording file --> <%! // Find a byte array in the byte array and return> = 0.-1 private int byteIndexOf (byte [] data, byte [] search, int start) is not returned) {int index =-1; int len = search. length; for (int I = start, j = 0; I <data. length; I ++) {int temp = I; j = 0; while (data [temp] = search [j]) {System. out. println (j + 1) + ", value:" + data [temp] + "," + search [j]); // count j ++; temp ++; if (j = len) {index = I; return index ;}} return index ;}%> <% String getAction = request. getParameter ("ac Tion "); if (getAction! = Null & (getAction. equals ("upload") // uploads a file {// defines the maximum number of uploaded file bytes 1 M int MAX_SIZE = 1024000; String rootPath; DataInputStream in = null; fileOutputStream fileOut = null; String remoteAddr = request. getRemoteAddr (); String serverName = request. getServerName (); String realPath = request. getRealPath ("/"); realPath = realPath. substring (0, realPath. lastIndexOf ("\"); // set the directory rootPath = realPath + "\ upload \ telRecord \" to save the file \\";/ /Obtain the data type String contentType = request. getContentType (); try {if (contentType. indexOf ("multipart/form-data")> = 0) {in = new DataInputStream (request. getInputStream (); int formDataLength = request. getContentLength (); if (formDataLength> MAX_SIZE) {out. println ("0, the file size exceeds the system limit! "); Out. flush (); return;} // Save the uploaded file Data byte dateBytes [] = new byte [formDataLength]; int byteRead = 0; int totalRead = 0; while (totalRead <formDataLength) {byteRead = in. read (dateBytes, totalRead, formDataLength); totalRead + = byteRead;} String data = new String (dateBytes, "UTF-8"); // get the uploaded file name String saveFile = data. substring (data. indexOf ("filename = \" ") + 10); saveFile = saveFile. substring (0, saveFile. indexOf ("\ n"); sa VeFile = saveFile. substring (saveFile. lastIndexOf ("\") + 1, saveFile. indexOf ("\" "); // retrieves the data split string int lastIndex = contentType. lastIndexOf ("="); // boundary = ----------------------------- String boundary = contentType. substring (lastIndex + 1, contentType. length (); // --------------------------- 257261863525035 // The length of the Data header at the beginning of the calculation int startPos; startPos = byteIndexOf (dateBytes, "filename = \"". getBytes (), 0); startPos = B YteIndexOf (dateBytes, "\ n ". getBytes (), startPos) + 1; // traverse 3 line breaks to the data block startPos = byteIndexOf (dateBytes, "\ n ". getBytes (), startPos) + 1; startPos = byteIndexOf (dateBytes, "\ n ". getBytes (), startPos) + 1; // boundary position int endPos = byteIndexOf (dateBytes, boundary. getBytes (), (dateBytes. length-startPos)-4; // create the File String fileName = rootPath + saveFile; File checkFile = new File (fileName); if (checkFile. exists () {out. println ("0, the file already exists! "); Out. flush (); return;} File fileDir = new File (rootPath); if (! FileDir. exists () fileDir. mkdirs (); // Write File fileOut = new FileOutputStream (fileName); fileOut. write (dateBytes, startPos, (endPos-startPos); fileOut. flush (); out. println ("File Uploaded successfully! Saved in: "+ fileName);} else {out. println (" 0, the uploaded file is not found! "); Out. flush (); return ;}} catch (Exception error) {out. println ("exception:" + error. getMessage ();} finally {try {if (in! = Null) in. close (); if (fileOut! = Null) fileOut. close () ;}catch (Exception e) {}} out. println ("<br>"); out. println ("<a href = 'uploadtelrecord. jsp '> continue upload </a> "); return ;}%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">