(转载)c# 发送http请求 到 servlet 跨平台 上传图像文件

最近经常用到c# 遇到一个需求。 由c#发送http请求流 到 servlet 接收文件流生成图片。  

 

原理: c# 获取文件  ---> 转化请求流 ---> 发送post提交 ----> servlet 接收输入流  --->生成文件

c#代码

 

1 String fileToUpload = "E:\\father.jpg";
2 //要发送请求的地址
3 String uploadUrl = "http://localhost:8080/NewSoft/MediaServlet";

 

1 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
2             webrequest.Method = "POST";
 1 FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
 2 webrequest.ContentLength = fileStream.Length;
 3 Stream requestStream = webrequest.GetRequestStream();
 4 //requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
 5 
 6 byte[] buffer = new Byte[(int)fileStream.Length];
 7 int bytesRead = 0;
 8 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
 9                 requestStream.Write(buffer, 0, bytesRead);
10  //requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
11 requestStream.Close();
12 
13 Console.ReadKey();

 

servlet代码

 

 1 public void doPost(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         //String path = request.getRealPath("/");
 4         String path = "E:\\";
 5 
 6         try {
 7             File file_path = new File(path);
 8             if (!file_path.exists()) {
 9                 file_path.mkdirs();
10             }
11             InputStream fin = request.getInputStream();
12             //System.out.println("key :"+ fin.read());
13             String file_path_name =path +(int)(Math.random()*1000)+".jpg";
14             //System.out.println("file_path_name:"+file_path_name);
15             File file = new File(file_path_name);
16             FileOutputStream file_out = new FileOutputStream(file);
17             int b;
18             while ((b = fin.read()) != -1) {
19                 file_out.write(b);
20             }
21             fin.close();
22             file_out.close();
23         } catch (Exception e) {
24            e.printStackTrace();
25         }
26     
27     }

 

 

转载:http://blog.csdn.net/nobodycanhelpme/article/details/7927083

 

最近经常用到c# 遇到一个需求。 由c#发送http请求流 到 servlet 接收文件流生成图片。  

 

原理: c# 获取文件  ---> 转化请求流 ---> 发送post提交 ----> servlet 接收输入流  --->生成文件

 

c#代码

 

[csharp]  view plain  copy
 
  1. String fileToUpload = "E:\\father.jpg";  
  2.            //要发送请求的地址  
  3.            String uploadUrl = "http://localhost:8080/NewSoft/MediaServlet";  
[csharp]  view plain  copy
 
  1. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);  
  2. webrequest.Method = "POST";  
[csharp]  view plain  copy
 
  1. FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);  
  2. webrequest.ContentLength = fileStream.Length;  
  3. Stream requestStream = webrequest.GetRequestStream();  
  4. //requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);  
  5.   
  6. byte[] buffer = new Byte[(int)fileStream.Length];  
  7. int bytesRead = 0;  
  8. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)  
  9.     requestStream.Write(buffer, 0, bytesRead);  
  10. //requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);  
  11. requestStream.Close();  
  12.   
  13. Console.ReadKey();  

 

 

servlet代码

 

 

[java]  view plain  copy
 
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.             throws ServletException, IOException {  
  3.         //String path = request.getRealPath("/");  
  4.         String path = "E:\\";  
  5.   
  6.         try {  
  7.             File file_path = new File(path);  
  8.             if (!file_path.exists()) {  
  9.                 file_path.mkdirs();  
  10.             }  
  11.             InputStream fin = request.getInputStream();  
  12.             //System.out.println("key :"+ fin.read());  
  13.             String file_path_name =path +(int)(Math.random()*1000)+".jpg";  
  14.             //System.out.println("file_path_name:"+file_path_name);  
  15.             File file = new File(file_path_name);  
  16.             FileOutputStream file_out = new FileOutputStream(file);  
  17.             int b;  
  18.             while ((b = fin.read()) != -1) {  
  19.                 file_out.write(b);  
  20.             }  
  21.             fin.close();  
  22.             file_out.close();  
  23.         } catch (Exception e) {  
  24.            e.printStackTrace();  
  25.         }  
  26.       
  27.     }  

转载于:https://www.cnblogs.com/qwer101652/p/5733257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值