ServletInputStream svlInStream = request.getInputStream(); System.out.println("ServletInputStream"); System.out.println(svlInStream.toString()); //System.out.println("File[" + file + "] name[" + widgetName + "] description[" + description + "]"); //int lastSepIndex = file.lastIndexOf("//"); //String filePath = file.substring(0, lastSepIndex); //String fileName = file.substring(lastSepIndex + 1); /************************************************************************************************************************************************* -----------------------------7d91a5c60b8a Content-Disposition: form-data; name="file"; filename="F:/a.wgt" Content-Type: text/plain zx_3.51.0 20090917 20090917 233442 20090917 235918 0 7 TableNO=00 20|20090917233442|8615622939812|0|20090917233442|20090917233442|09172334580000091000||0|1|15622939812|4900|15622939812|4900|15622939812|4900|0|0|000101|0|000001|2|1|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|56|1562293981267627|||05|90085|05000168|1| |||0500016801||||0|1|1|0|0|0|3|1|0|0||0|0|1|0|0|0|0|1|0|1|99000807| |1000003473|0||| ||| 20|20090917234201|8615602600298|0|20090917234201|20090917234201|09172342410000091000||0|1|15602600298|4900|15602600298|4900|15602600298|4900|0|0|000101|0|000001|3|2|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|56|1560260029898092|||05|90162|05000194|1| |||0500019401||||0|1|1|0|0|0|3|1|0|0||0|0|1|0|0|0|0|1|0|1|99000859||1000003357|0||| ||| 20|20090917235023|8615602322248|0|20090917235023|20090917235023|09172350390000091000||0|1|15602322248|4900|15602322248|4900|15602322248|4900|0|0|000101|0|000001|2|1|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|56|1560232224876869|||05|90085|05000169|1| |||0500016901||||0|1|1|0|0|0|3|1|0|0||0|0|1|0|0|0|0|1|0|1|99000809| |1000003461|0||| ||| 20|20090917235129|8613113302402|0|20090917235129|20090917235129|09172351450000091000||0|1|13113302402|4900|13113302402|4900|13113302402|4900|0|0|000101|0|000001|2|1|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|51|1311330240275917|||05|90300|05000240|1| |||0500024002||||0|1|1|0|0|0|3|1|0|0||0|0|1|0|0|0|0|1|0|1|99000953| |1000003179|0||| ||| 20|20090917235246|8618602086792|0|20090917235246|20090917235246|09172353020000091000||0|1|18602086792|4900|18602086792|4900|18602086792|4900|0|0|000101|0|000001|2|1|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|56|1860208679239544|||05|91278|05000578|1| |||0500057801||||0|1|1|0|0|0|3|1|0|0||0|0|1|0|0|0|0|1|0|1|99001629| |1000001825|0||| ||| 20|20090917235741|8615622211377|0|20090917235741|20090917235741|0917235757mtv7742||0|1|15622211377|WCDMA|15622211377|WCDMA|15622211377|WCDMA|0|0|000101|0|000001|2|1|23|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|51|1562221137724171|||80|90120|80005029|1| |||8000502901||||0|1|1|0|0|0|5|1|0|0||0|0|1|0|1|0|0|1|0|1|99000429| |1000000690|0|90120|| ||| 20|20090917235918|8613226129913|0|20090917235918|20090917235918|09172353410000991118||0|1|13226129913|GSM|13226129913|GSM|13226129913|GSM|0|39|039002|0|000001|2|1|10|0|0|-1|-1|-1|-1|-1|0||1|0|1|0|0|0|-1|0|56|1322612991356346|||05|32729|05514895|1| |||0551489503||||0|1|1|0|0|0|3|1|0|0||0|0|2|0|1|0|0|1|0|1|303| |303|0||| ||| -----------------------------7d91a5c60b8a Content-Disposition: form-data; name="name" dsafd -----------------------------7d91a5c60b8a Content-Disposition: form-data; name="description" DSAFD -----------------------------7d91a5c60b8a-- **************************************************************************************************************************************************/ FileUploader uploader = new FileUploader(); /* File f = new File("F://xxx.txt"); FileOutputStream outStream = new FileOutputStream (f); int oneByte = svlInStream.read(); while(oneByte != -1){ outStream.write(oneByte); oneByte = svlInStream.read(); } outStream.close(); */ String divideLine = null; String bufferStr = null; byte[] ioBuffer = new byte[128]; int readLen = -1; int wirteLen = -1; int firstPosition = -1; int lastPosition = -1; //第一行,流不同部分分隔行 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); bufferStr = new String(ioBuffer, 0, readLen); lastPosition = bufferStr.lastIndexOf("/r/n"); System.out.println("bufferStr[" + bufferStr + "]"); System.out.println("lastPosition[" + lastPosition + "]"); divideLine = bufferStr.substring(0, lastPosition); System.out.println("divideLine[" + divideLine + "]"); //第二行,从中解析出上传文件名 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); bufferStr = new String(ioBuffer, 0, readLen); String filenameIndexStr = new String("filename="); firstPosition = bufferStr.indexOf(filenameIndexStr) + filenameIndexStr.length() + 1; lastPosition = bufferStr.lastIndexOf("/""); file = bufferStr.substring(firstPosition, lastPosition); System.out.println("Fist positon[" + firstPosition + "] Last position[" + lastPosition + "]"); System.out.println("file[" + file + "]"); firstPosition = file.lastIndexOf("//"); String fileName = file.substring(firstPosition + 1); System.out.println("fileName[" + fileName + "]"); OutputStream outStream = uploader.GetOutputStream(fileName); //第三行,从中解析出是否上传文件 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); System.out.println("readLen[" + readLen + "]"); bufferStr = new String(ioBuffer, 0, readLen); System.out.println(bufferStr); /* if(bufferStr.indexOf(new String("text/plain")) < 0){ System.out.println("Client does not upload file"); PrintWriter out =response.getWriter(); out.println("<mce:script language='javascript'><!-- alert('请上传Widget应用程序文件!'); // --></mce:script>"); return; } */ //第四行,空行,不处理 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); System.out.println("readLen[" + readLen + "]"); System.out.println(new String(ioBuffer, 0, readLen)); //第五行,开始读取文件主体 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); System.out.println("readLen[" + readLen + "]"); System.out.println(new String(ioBuffer, 0, readLen)); System.out.println("To parse file!"); while(-1 != readLen){ bufferStr = new String(ioBuffer, 0, readLen); //System.out.print(bufferStr); if(bufferStr.indexOf("/r/n") >= 0){ //System.out.println("11111111111111111111111"); byte[] tempBuffer = new byte[128]; String tempStr = null; int tempReadLen = -1; tempReadLen = svlInStream.readLine(tempBuffer, 0, tempBuffer.length); //继续读一行,如果新读的行是文件体部分结束符,“/r/n”不写入流;否则,写入输出流 tempStr = new String(tempBuffer, 0, tempReadLen); if(tempStr.indexOf(divideLine) >= 0){ //将最后一行除了“/r/n”外的其它字节写入输出流 outStream.write(ioBuffer, 0, readLen - 2); outStream.flush(); //System.out.println("333333333333333333333333"); break; } else{ //System.out.println("22222222222222222222222"); outStream.write(ioBuffer, 0, readLen); outStream.flush(); //继续处理刚读出来的行 readLen = tempReadLen; for(int i = 0; i < readLen; i++){ ioBuffer[i] = tempBuffer[i]; } continue; } } outStream.write(ioBuffer, 0, readLen); //System.out.println("00000000000000000000000"); outStream.flush(); readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); } outStream.close(); System.out.println("End parse file!"); if(-1 == readLen){ System.out.println("Read -1 bytes!"); } //标示行,Content-Disposition: form-data; name= readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); //空行 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); //Widget应用名称行 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); bufferStr = new String(ioBuffer, 0, readLen); lastPosition = bufferStr.lastIndexOf("/r/n"); widgetName = bufferStr.substring(0, lastPosition); System.out.println("Widget name[" + widgetName + "]"); //读到流的新部分 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); //标示行,Content-Disposition: form-data; name= readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); //空行 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); //应用描述行 readLen = svlInStream.readLine(ioBuffer, 0, ioBuffer.length); bufferStr = new String(ioBuffer, 0, readLen); lastPosition = bufferStr.lastIndexOf("/r/n"); description= bufferStr.substring(0, lastPosition); System.out.println("description[" + description + "]");