1

我使用这个 httpclient 将图像发布到我的 grails,如下所示。如何在 grails 中接收文件?

public static String webPost(String method, File data, Context c) throws Exception {

    String json = "";

    if (isOnline(c) == true) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost httppost ; 
        try {
            httppost = new HttpPost(method);
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("image", new FileBody(data));
            httppost.setEntity(entity);
            response = httpclient.execute(httppost);

               if (response != null) {
                HttpEntity r_entity = response.getEntity();
                json = EntityUtils.toString(r_entity);
              Log.i("ja", json);
            } 

        } catch (Exception e) {
            throw new RuntimeException(e.getLocalizedMessage());
        } finally {
            httpclient = null;
            response = null;
            httpclient = null;
        }
    } else {
        throw new RuntimeException("No internet connection");
    }
    return json;

}

我的圣杯:

  def image = request.getFile('image')
   image.transferTo(new File('c:/p.png') )

错误:

groovy.lang.MissingMethodException: No signature of method: org.apache.catalina.core.ApplicationHttpRequest.getFile() is applicable for argument types: (java.lang.String) values: [image]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getInfo(), recycle()
    at mclient.TestController$_closure1.doCall(TestController.groovy:10)
    at mclient.TestController$_closure1.doCall(TestController.groovy)
    at java.lang.Thread.run(Thread.java:662)
4

0 回答 0