httpurlconnection发送文件到服务端并接收_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > httpurlconnection发送文件到服务端并接收

httpurlconnection发送文件到服务端并接收

 2011/11/4 8:11:19  hunray  http://hunray.iteye.com  我要评论(0)
  • 摘要:客户端代码importjava.io.DataInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;/**@authorxk*上传文件到文件服务器的客户端*/publicclassFileUploadClient{publicstaticvoidmain
  • 标签:服务端 文件 URL 服务
客户端代码


import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*
 * @author xk
 * 上传文件到文件服务器的客户端
 */
public class FileUploadClient{
	 public static void main(String[] args) {
	   try {
//		   URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
		   URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
		   HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		   // 发送POST请求必须设置如下两行

		   conn.setDoOutput(true);
		   conn.setUseCaches(false);
		   conn.setRequestMethod("POST");
		   conn.setRequestProperty("Content-Type","text/html");
           conn.setRequestProperty("Cache-Control","no-cache");
           conn.setRequestProperty("Charsert", "UTF-8"); 
           conn.connect();
           conn.setConnectTimeout(10000);
           OutputStream out =conn.getOutputStream();

		   File file = new File("C:\\fileSys\\test.zip");
 
		   DataInputStream in = new DataInputStream(new FileInputStream(file));

		   int bytes = 0;
		   byte[] buffer = new byte[1024];
		   while ((bytes = in.read(buffer)) != -1) {
			   out.write(buffer, 0, bytes);
		   }
		   in.close();
		   out.flush();
		   out.close(); 
	
		   conn.getInputStream();
		   conn.disconnect();
	   } catch (Exception e) {
	   		System.out.println("发送文件出现异常!" + e);
	   		e.printStackTrace();
	   }
	}
}


服务端代码,接收文件

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.property.PropertyUtil;

public class GetFileAction extends ActionSupport {

	private static final long serialVersionUID = -9108080625441081032L;  
	public String execute() throws Exception   
	{   	
		String rootPath = PropertyUtil.getProperty("fileSys.properties","fileSys.path");
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String filePath = request.getParameter("filePath");
		String fileName = request.getParameter("fileName");

		InputStream input = request.getInputStream(); 
		String fileFullPath = rootPath + filePath + fileName; 
		
		File file = new File(rootPath + filePath);
		if(!file.exists()){
			file.mkdirs();
		}
		FileOutputStream fos = new FileOutputStream(fileFullPath);

		int size = 0;
		byte[] buffer = new byte[1024];
		while ((size = input.read(buffer,0,1024)) != -1) {
			fos.write(buffer, 0, size);
		}
		fos.close();
		input.close();

		return Action.NONE;   
	}   
}


发表评论
用户名: 匿名