post 上传下载文件流

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class HttpRequestUtils {
	public static HttpResponse getHttpResponse(String url, Map<String, String> params) throws Exception{
		HttpParams httpParameters = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParameters, 2 * 60 * 100);
		HttpConnectionParams.setSoTimeout(httpParameters, 2 * 60 * 100);
		DefaultHttpClient client = new DefaultHttpClient(httpParameters);
		HttpGet httpGet = new HttpGet(new URI(url));
		for(Map.Entry<String, String> entry : params.entrySet()){
			httpGet.setHeader(entry.getKey(), entry.getValue());
		}
		return client.execute(httpGet);
	}
	
	public static HttpURLConnection getUrlConnection(String url, int bufferSize) throws Exception{
		URL target = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) target.openConnection();
		conn.setConnectTimeout(30000);
		conn.setReadTimeout(30000);
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setChunkedStreamingMode(bufferSize);
		conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
		conn.setRequestProperty("Charsert", "UTF-8");
		conn.setRequestProperty("contentType", "UTF-8");
		conn.setRequestProperty("timenow", String.valueOf(System.currentTimeMillis()));
		return conn;
	}
	
	
}

上传文件流关键代码:

private static void uploadFile() throws Exception
	{
		//BUFFER_SIZE = 16 * 1024 ;
		File file = new java.io.File("D:/workbook.xlsx");
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		HttpURLConnection conn=HttpRequestUtils.getUrlConnection("http://localhost:7080/test/FileUploadAndDownloadServlet", Constants.BUFFER_SIZE);
		conn.setRequestProperty("type","1");
		conn.setRequestProperty("source", "3486123"); //文件目录
		conn.setRequestProperty("range", String.valueOf(0)); 
		conn.setRequestProperty("filename", "dison.xlsx"); //文件名称
		conn.setRequestProperty("totalsize", String.valueOf(file.length())); //文件大小
		conn.setRequestProperty("tousername", "dison"); //接收者
		conn.setRequestProperty("fromusername", "dison1");//发送者
		
		RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rw"); //文件流key
		OutputStream out = new DataOutputStream(conn.getOutputStream());
		
		int j=0;
		byte[] buf = new byte[Constants.BUFFER_SIZE];
		int nRead=0;
		try {
			randomAccessFile.seek(Long.valueOf(0));//设置文件指针
			while((nRead=randomAccessFile.read(buf,0,Constants.BUFFER_SIZE)) > 0)
			{
				out.write(buf,0,nRead);
				j += nRead;
			}
			out.close();
			randomAccessFile.close();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				randomAccessFile.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
			
		 	out.flush();
		 	out.close();
		 	// 定义BufferedReader输入流来读取URL的响应
		 	BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
		 	String line = null;
		 	while ((line = reader.readLine()) != null) {
		 		System.out.println(line);
		 	}
	
	}

接收文件流 (关键代码)部分代码:

主要逻辑:

1.首先确定你创建的目录是否存在。没有就创建

2.通过文件流的key读取流,创建文件

RandomAccessFile randomAccessFile;
		try {
			randomAccessFile = new RandomAccessFile(destfile, "rw");
		} catch (FileNotFoundException e) {
			logger.error(e);
			fileUploadResult = new FileUploadResult(false,FileBizCodeConstants.FILEID_UNEXIST, "文件未找到");
			return fileUploadResult;
		}
		byte[] buf = new byte[FileBizCodeConstants.BUFFER_SIZE];
		int nRead;
		int stu=0;
		
			
			randomAccessFile.seek(Long.valueOf(range));//设置文件指针
			while((nRead=is.read(buf,0,FileBizCodeConstants.BUFFER_SIZE)) > 0&&stu==0)
			{
				randomAccessFile.write(buf,0,nRead);
				l += nRead;
			}

相关推荐