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

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

客户端

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;   
    }   
}

 

如果用jersey,形成RESTfull webservices 就更有用了

 

posted on 2015-07-07 16:25  rojas  阅读(565)  评论(0编辑  收藏  举报