简体   繁体   English

将带有发布请求的文件信息发送到Google Appengine

[英]sending file information with post request to google appengine

I am trying to write a Google Appengine Post program that receive a file. 我正在尝试编写一个接收文件的Google Appengine Post程序。

I observe that I get the content length of six bytes which is what the client sent. 我观察到我得到了六个字节的内容长度,这是客户端发送的内容。 However, I cannot get the content. 但是,我无法获取内容。 When I try to read from the getInputStream I get -1 the first time. 当我尝试从getInputStream读取时,我第一次得到-1。 (I also tried reading into a ByteArray of six bytes, and similarly, I get nothing in the buffer.) (我也尝试读取六个字节的ByteArray,同样,缓冲区中什么也没得到。)

package guestbook;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.*;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import java.util.logging.Logger;
public class XStore extends HttpServlet {
  public void doPost (HttpServletRequest req, HttpServletResponse resp) throws
     IOException {
    Logger log = Logger.getLogger(XStore.class.getName());
     String ct;
     ct=req.getContentType();
     resp.setContentType("text/plain");
     String key = req.getParameter("key");
     String value = req.getParameter("value");
     DatastoreService d = DatastoreServiceFactory.getDatastoreService();
     byte [] B;
     InputStream IS = req.getInputStream();
int i,le;
le = req.getContentLength(); 
StringBuffer rB = new StringBuffer();
int rv;
int c;
c = IS.read();
log.info("Srv = " + c ); 

This returns logs a -1 ... rest of program omitted for brevity My displays also indicated that le was six. 此返回日志-1 ...为简洁起见省略了程序的其余部分。我的显示还显示le为6。

Here is the client 这是客户

import java.net.*;
import java.io.*;
public class client {
public static void main (String a []  ) throws MalformedURLException,java.io.IOException,ProtocolException {
URL u = new URL("http://uthreee.appspot.com/XX?key=a&value=a");
HttpURLConnection H;
H = (HttpURLConnection) u.openConnection();
H.setRequestMethod("POST");
H.setDoOutput(true);
OutputStreamWriter w = new OutputStreamWriter(H.getOutputStream());
w.write("abc");
w.write("def");
w.close();
System.out.println(H.getResponseCode());
}}

Note that the response code is 200. Thanks for any insight. 请注意,响应代码为200。感谢您的见解。 A student and myself spent over ten hours together trying to find a way to transmit an XML file to the Google Appengine, but cannot find a way to read the information sent by the client (which ultimately will be an Android App). 一名学生和我本人花了十多个小时一起试图找到一种将XML文件传输到Google Appengine的方法,但是却找不到一种读取客户端发送的信息的方法(最终将是一个Android App)。

Dr. Laurence Leff Associate Professor of Computer Science, Western Illinois University, Macomb IL 61455 on sabbatical Laurence Leff博士,西伊利诺伊大学计算机科学副教授,Macomb IL 61455,休假

For what it's worth, using a browser form containing an <input type="file">, the Apache Commons FileUpload definitely works with AppEngine. 值得一提的是,使用包含<input type =“ file”>的浏览器表单, Apache Commons FileUpload肯定可以与AppEngine一起使用。 In my case the form seemed to require the enctype="multipart/form-data" attribute, and of course method="post". 在我的情况下,表单似乎需要enctype =“ multipart / form-data”属性,当然还需要method =“ post”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM