자바 업로드

6685 단어 자바upload
자바 오 랜 만 에 쓰 는 거 예요.
표준 httpServletRequest 가 아 닌 서버 를 만 났 습 니 다.
common-uproad 구성 요 소 를 사용 할 수 없습니다.
슬프다
해석 하 다
request.getInputStream()이 이렇게 하 는 구나.

Map<String, String> formfieldsTable = new HashMap<String, String>();
		String fileRealName = ""; //          
		String length="";//  
		try {

			response.setContentType("text/html;charset=UTF-8");
			//PrintWriter out = response.getWriter();
			final int NONE = 0; //    ,        
			final int DATAHEADER = 1; //             
			final int FILEDATA = 2; //                   
			final int FIELDDATA = 3; //               
			//           (                )
			int totalbytes = request.getContentLength();
			File f; //            
			//              
			byte[] dataOrigin = new byte[totalbytes];
			//   post       ,b                    
			byte[] b = new byte[totalbytes];
			//       
			String contentType = request.getContentType();
			String fieldname = ""; //       
			String fieldvalue = ""; //      
			String fileFormName = ""; //             
			
			String boundary = ""; //       
			String lastboundary = ""; //         
			int fileSize = 0; //     
			//         /     
			
			//         /       
			Map<String, String> filenameTable = new HashMap<String, String>();
			//                
			int pos = contentType.indexOf("boundary=");
			int pos2; // position2
			if (pos != -1) {
				pos += "boundary=".length();
				boundary = "--" + contentType.substring(pos); //       
				lastboundary = boundary + "--"; //        
			}
			int state = NONE; //      NONE
			//             
			DataInputStream in = new DataInputStream(request.getInputStream());
			in.readFully(dataOrigin); //     ,              dataOrigin 
			in.close(); //      
			String reqcontent = new String(dataOrigin); //                 
			//             
			BufferedReader reqbuf = new BufferedReader(new StringReader(
					reqcontent));
			//       
			boolean flag = true;
			// int i = 0;
			while (flag == true) {
				String s = reqbuf.readLine();
				if (s == lastboundary || s == null)
					break;
				switch (state) {
				case NONE:
					if (s.startsWith(boundary)) {
						//        ,           
						state = DATAHEADER;
						// i += 1;
					}
					break;
				case DATAHEADER:
					pos = s.indexOf("filename=");
					//                  ,            
					if (pos == -1) {
						//             ,         
						pos = s.indexOf("name=");
						pos += "name=".length() + 1; // 1     "   
						s = s.substring(pos);
						int l = s.length();
						s = s.substring(0, l - 1); //    "
						fieldname = s; //         fieldname
						System.out.print("fieldname=" + fieldname);
						state = FIELDDATA; //      ,         
					} else {
						//          ,      ,          
						String temp = s;
						//        
						pos = s.indexOf("name=");
						pos += "name=".length() + 1; // 1     "   
						pos2 = s.indexOf("filename=");
						String s1 = s.substring(pos, pos2 - 3); // 3  ";      
						fileFormName = s1;
						pos2 += "filename=".length() + 1; // 1     "   
						s = s.substring(pos2);
						int l = s.length();
						s = s.substring(0, l - 1);
						pos2 = s.lastIndexOf("\\"); //   IE      
						s = s.substring(pos2 + 1);
						fileRealName = s;
						System.out.print("fileRealName=" + fileRealName + "<br>");
						System.out.print("fileRealName.length()="
								+ fileRealName.length() + "<br>");
						if (fileRealName.length() != 0) { //         
							//                    
							b = dataOrigin; //             
							pos = byteIndexOf(b, temp, 0); //    
							//      ,2                 
							b = subBytes(b, pos + temp.getBytes().length + 2,
									b.length);
							//       ,        Content-type
							s = reqbuf.readLine();
							//        ,     
							f = new File(
									"/usr/local/app/author_portal_front/upload"
											+ File.separator + fileRealName);
							DataOutputStream fileout = new DataOutputStream(
									new FileOutputStream(f));
							//          ,4        4   ,       2   ,Content-type  
							//             , 2   
							//            
							b = subBytes(b, s.getBytes().length + 4, b.length);
							pos = byteIndexOf(b, boundary, 0); //          
							b = subBytes(b, 0, pos - 1); //       
							length=b.length+"";
							fileout.write(b, 0, b.length - 1); //        
							fileout.close();
							fileSize = b.length - 1; //       fileSize
							System.out.print("fileFormName=" + fileFormName
									+ " filename=" + fileRealName
									+ " fileSize=" + fileSize + "<br>");
							filenameTable.put(fileFormName, fileRealName);
							state = FILEDATA;
						}
					}
					break;
				case FIELDDATA:
					//        
					s = reqbuf.readLine();
					fieldvalue = s; //   fieldvalue
					System.out.print(" fieldvalue=" + fieldvalue + "<br>");
					formfieldsTable.put(fieldname, fieldvalue);
					state = NONE;
					break;
				case FILEDATA:
					//             ,     
					while ((!s.startsWith(boundary))
							&& (!s.startsWith(lastboundary))) {
						s = reqbuf.readLine();
						if (s.startsWith(boundary)) {
							state = DATAHEADER;
						} else {
							break;
						}
					}
					break;
				}
			}
		} catch (Exception e) {
			System.out.println("error upload:" + e);
			e.printStackTrace();
		}
		String abc=GetTest.testGet("/usr/local/app/author_portal_front/upload/"+fileRealName , formfieldsTable.get("myname"),length);
		System.out.println("------------->uploadUrl:"+abc);
		request.setAttribute("intro", abc);

좋은 웹페이지 즐겨찾기