java后台上传文件

 HttpClient httpclient = new DefaultHttpClient();  

         

     try {  

           HttpPost httppost = new HttpPost("http://192.168.1.120:8080/xcjedu/oper/course/upload");  

           FileBody bin = new FileBody(new File("E:\\WBT1.xml"));  

           StringBody comment = new StringBody("just test");  

           MultipartEntity reqEntity = new MultipartEntity();  

           reqEntity.addPart("fileUpload", bin);//upload为请求后台的File upload;属性  

//            reqEntity.addPart("str", comment);//str 为请求后台的String str;属性  

           httppost.setEntity(reqEntity);  

           HttpResponse response = httpclient.execute(httppost);  

           int statusCode = response.getStatusLine().getStatusCode();  

           if(statusCode == HttpStatus.SC_OK){  

               HttpEntity resEntity = response.getEntity();  

               System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据  

               EntityUtils.consume(resEntity);  

           }  

       } finally {  

           try {   

               httpclient.getConnectionManager().shutdown();   

           } catch (Exception e) { 

           e.getMessage();

           }  

       }  

你可能感兴趣的:(java)