java post音频文件100K左右时正常运行 900K左右时返回500错误再大3M多writting error to sever

出问题都是这一行 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码

具体的问题

当post文件900K时候出现异常

剩余jvm内存53843792

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=ja-jpat sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)at readflac.main(readflac.java:57)

剩余jvm内存43074296当post文件3M时出现异常。

Exception in thread "main" java.io.IOException: Error writing to serverat sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468)at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:480)at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1062)at readflac.main(readflac.java:57)

具体就是想要通过post音频区谷歌接口返回的是文本,求各位大神解答。另外如果有什么别的好的实现方法的话万望赐教。本人入门小菜。谢谢其中异常上面那个数字是剩余内存。。虚拟机的。应该不是不足的原因吧。希望各位指点一二。应该是传输的内容太大引发 的什么错误?

import java.awt.datatransfer.StringSelection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Spring;


public class readflac {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println(new File("").getAbsolutePath());
		String url = "http://www.google.com/speech-api/v1" +
				"/recognize?xjerr=1&client=chromium&lang=ja-jp";
		URL postUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
		// 设置是否向connection输出,因为这个是post请求,参数要放在
        // http正文内,因此需要设为true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        connection.setDoInput(true);
        // Set the post method. Default is GET
        connection.setRequestMethod("POST");
        // Post cannot use caches
        // Post 请求不能使用缓存
        connection.setUseCaches(false);
        // This method takes effects to
        // every instances of this class.
        // URLConnection.setFollowRedirects是static函数,作用于所有的URLConnection对象。
        // connection.setFollowRedirects(true);
 
        // This methods only
        // takes effacts to this
        // instance.
        // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
        connection.setInstanceFollowRedirects(true);
		// TODO Auto-generated method stub
        connection.setRequestProperty("Content-Type","audio/x-flac; rate=16000");
        connection.connect();
        DataOutputStream outputStream  = new DataOutputStream(connection.getOutputStream());
        outputStream.write(readfileByYte("aaaa.flac"));
        outputStream.flush();
        outputStream.close();
        System.out.println("dsfgdfsgdsfgshengyu"+Runtime.getRuntime().freeMemory());;
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
        System.out.println("dsfgdfsgdsfgshengyu"+Runtime.getRuntime().freeMemory());;

        getInfoFromJson(null, reader);
       
       
        reader.close();
        connection.disconnect();
        
        

	}
	public static File readFlacByByte(String fileName) {
		File file = new File(fileName);
		InputStream input = null;
		try {
			System.out.println("yi字节的方式读取文件");
			input = new FileInputStream(file) ;
			int tempByte = 0;
			while ((tempByte = input.read())!= -1) {
				System.out.println(tempByte);
				
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		return file;
		
	}
	public static byte[] readfileByYte(String filename) throws IOException {
		File file = readFlacByByte(filename);
		long len = file.length();
		byte[] voice = new byte[(int) len];
		FileInputStream fileInputStream = new FileInputStream(file);
		fileInputStream.read(voice, 0, (int)len);
		fileInputStream.close();
		return voice;
		
	}
	public static void  getInfoFromJson(String jsonString,BufferedReader reader) throws IOException {
		 String line="";
	        String lineTemp = "";
	        
	        
			while ((line = reader.readLine()) != null){
	            //line = new String(line.getBytes(), "utf-8");
	            System.out.println(line);
	            lineTemp+=line;
	        }
	        String[] StringSelection = lineTemp.split(",");
	        for (int i = 0; i < StringSelection.length; i++) {
				System.out.println(StringSelection[i]);
			}
	        String[] strings = StringSelection[2].split("\"");
	        String resultString = strings[strings.length-1];
	        if (StringSelection[0].charAt(StringSelection[0].length()-1)=='0') {
				System.out.println("匹配成功 结果为:"+resultString);
			}
	        else {
				System.out.println("匹配失败重新匹配");
			}
	        System.out.println(StringSelection.length);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值