J 2 eeブレークポイント継続及びpost要求にはurlパラメータ及びファイルフローが含まれています。

4447 ワード

まずは
client端:
HttpURLConnection conn = null;
		BufferedInputStream fin = null;
		BufferedOutputStream out = null;
		URL reqUrl;
		try {
			reqUrl = new URL("http://<ip>:<port>/Emergency/phone/attachmentUpload");
			conn = (HttpURLConnection) reqUrl.openConnection();
			conn.setConnectTimeout(3000);
			conn.setRequestMethod("PUT");
			if(isCommit){
				conn.setRequestProperty("isCommit", "true");
			}else{
				conn.setRequestProperty("isCommit", "false");
			}
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("Content-Type", "binary/octet-stream");
			conn.setRequestProperty("offset", StringUtil.toString(offset));
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 1M chunk  
			conn.setChunkedStreamingMode(1024*1024);
			out = new BufferedOutputStream(conn.getOutputStream());
			fin = new BufferedInputStream(new FileInputStream(file));
			byte[] buf = new byte[bufferSizeUpload];
			int len = -1;
			long currentUploadSize = offset;
			fin.skip(offset);
			while ((len = fin.read(buf)) != -1&currentUploadSize<offset+uploadSize) {
				if(offset+uploadSize-currentUploadSize<bufferSizeUpload){
					len = Integer.parseInt(StringUtil.toString(offset+uploadSize-currentUploadSize));
				}
				if(len>0){
					if(out!=null){
						out.write(buf, 0, len);
						out.flush();
					}
				}
				currentUploadSize += len;
			}
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
		} catch(IOException e){
			e.printStackTrace();
		} finally {
			try {
				if (fin != null) {
					fin.close();
					fin = null;
				}
				if (out != null) {
					out.close();
					out = null;
				}
				if (conn != null) {
					conn.disconnect();
					conn = null;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
				throw new RuntimeException("Release resource failed.");
			}
		}
上述のように、urlパラメータは、set Request Property方法により要求ヘッダに置かれ、ファイルはbodyに流される。
同時に注意が必要です HttpURLConnection.set ChunkedStreamingModeこの方法は、ファイルフローが指定サイズに達するたびに一回送信されることを保証し、キャッシュに置かれ、一回の転送で遭遇する可能性のあるデータの欠落を回避します。
http://blog.csdn.net/z69183787/article/details/8186918 参考になる
サービス:
public String underUpload() throws IOException{
		String data = request.getHeader("data"); 
		String userInfo = request.getHeader("userInfo"); 
		userInfo = new String(userInfo.getBytes("iso-8859-1"),"GBK");
		//System.out.println(data);
		//System.out.println(userInfo);
		Map<String,String> data_map = gson.fromJson(data,attachVo.data_map.getClass());
		if(data_map!=null) attachVo.data_map=data_map;
		attachVo.user = gson.fromJson(userInfo,attachVo.user.getClass());
		apiService.attachUpload(attachVo,new BufferedInputStream(request.getInputStream()));
		//response.setStatus(200);
		return null;
	}
sshアーキテクチャを使用して、get Headerを通じてurlパラメータを得て、get InputStreamを通じてファイルストリームを得るとともに、中国語の文字化けを防ぐためにコードを制御する必要があります。
ブレークポイントの継続については、基本的な考え方は、情報ヘッダの部分を通して転送毎のファイルサイズを伝達し、サーバ端のファイルサイズと一致する。を選択して、
RandomAccessFile raFile = new RandomAccessFile(uploadFile.getAbsolutePath(), "rw");
		raFile.seek(uploadFile.length());
方法は伝送サイズに応じてリアルタイムでファイルの読み書きを行う。
public static void saveFile(String destFilePathStr,String destFileName){
		try {
			File destFilePath = new File(destFilePathStr);
			if(!destFilePath.exists()){
				destFilePath.mkdirs();
				destFilePath = null;
			}
			File destFile = new File(destFilePathStr+"//"+destFileName);
			if(!destFile.exists()){
				destFile.createNewFile();
			}
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static long uploadFile(String offset,String destFileName,BufferedInputStream bis) 
	throws IOException{
		File uploadFile = new File(destFileName);
		int len = 0;
		byte[] bt = new byte[1024];
		RandomAccessFile raFile = new RandomAccessFile(uploadFile.getAbsolutePath(), "rw");
		raFile.seek(uploadFile.length());
	    while ((len = bis.read(bt)) > 0){
	    	raFile.write(bt, 0, len);
    	}
	    long l = raFile.length();
	    try {
		     if(bis != null)
		    	 bis.close();
		     if (raFile != null)
		    	 raFile.close();
		     
		} catch (IOException e) {
			l = 0;
		    e.printStackTrace();
		}
		return l ;
	}
まず空のファイルを作成し、次に受信するファイルのストリームとファイルの長さに従ってファイルに書き込みます。