最近经常用到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#代码
- String fileToUpload = "E:\\father.jpg";
- //要发送请求的地址
- String uploadUrl = "http://localhost:8080/NewSoft/MediaServlet";
- HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
- webrequest.Method = "POST";
- FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
- webrequest.ContentLength = fileStream.Length;
- Stream requestStream = webrequest.GetRequestStream();
- //requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
- byte[] buffer = new Byte[(int)fileStream.Length];
- int bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- requestStream.Write(buffer, 0, bytesRead);
- //requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
- requestStream.Close();
- Console.ReadKey();
servlet代码
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //String path = request.getRealPath("/");
- String path = "E:\\";
- try {
- File file_path = new File(path);
- if (!file_path.exists()) {
- file_path.mkdirs();
- }
- InputStream fin = request.getInputStream();
- //System.out.println("key :"+ fin.read());
- String file_path_name =path +(int)(Math.random()*1000)+".jpg";
- //System.out.println("file_path_name:"+file_path_name);
- File file = new File(file_path_name);
- FileOutputStream file_out = new FileOutputStream(file);
- int b;
- while ((b = fin.read()) != -1) {
- file_out.write(b);
- }
- fin.close();
- file_out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }