springlin
发贴: 1
积分: 0
|
于 2006-05-09 11:30
我现在要在客户端实现使用HTTP向服务器传输文件功能,请各位高手帮忙看一下我写的这段代码是否可以?不行的话请指教,谢谢! import java.io.*; import java.net.*; public class PostExample { public static void main(String[] argv) throws Exception { URL url = new URL("http://168.1.100.29:8080/web/servlet/loginServlet");
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST");
connection.setDoOutput(true); BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream()); //send the encoded message File file=new File("d:\\aa.doc"); FileInputStream fr=new FileInputStream(file); int bb; byte[] b=new byte[1024]; while((bb=fr.read)!=-1){ out.write(b,0,bb); } fr.close(); out.close(); }
|