HttpURLconnection 给Struts2 的Action发一个post请求,在Action里面的通过HttpServletRequest得到一个inputStream来获取HttpURLconnection 发过来的数据。现在的问题是inputStream 可读的数据为0.这个问题是不是Struts框架自动解析参数有关,还是其他的,求大神赐教。
接受请求的 Action
package com.luojia.zwc.test;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
public String execute() throws IOException{
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
int len = req.getContentLength();
System.out.println(len);
InputStream input = req.getInputStream();
System.out.println(input.available());
byte[] buff = new byte[len];
input.read(buff);
System.out.println(new String(buff));
input.close();
//res.setStatus(HttpServletResponse.SC_OK);
return SUCCESS;
}
}
发送post请求的客户端
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUploadClient {
public static void main(String[] args) throws IOException{
URL url= new URL("http://localhost:8080/test001/fileupload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(500000);
conn.setReadTimeout(500000);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
//conn.setRequestProperty("Charsert", "UTF-8");
//conn.setRequestProperty("content-type","text/html");
//conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
String str="d://mysql.sql";
conn.connect();
File file = new File(str);
InputStream input = new DataInputStream(new FileInputStream(file));
System.out.println(input.available());
byte[] buff = new byte[1024];
int len=0;
OutputStream output = conn.getOutputStream();
//System.out.println(conn.getOutputStream());
while((len=input.read(buff,0,1000))>0){
output.write(buff,0,len);
output.flush();
}
input.close();
output.close();
InputStream in = conn.getInputStream();
in.close();
conn.disconnect();
}
}
------解决方案--------------------
没玩过HttpURLConnection 发送请求。楼主担心Struts框架自动解析参数有关,那就在工程里面配置一个servlet,看看servlet能不能获取到request里面请求的内容。如果还获取不到,那可能就是HttpURLConnection 发送请求的问题了。
------解决方案--------------------
参考这个。
------解决方案--------------------
public class TestPage extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
}
------解决方案--------------------
楼主有没有尝试将 conn.setRequestProperty 添加上去struts2里面去取流数据呢?
//需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数
conn.setRequestProperty("content-type", "text/html"); //直接传递流对象
//以下的则是通过form组件的形式来传递流对象的,具体使用上网查看。
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
刚才搜了下,网上没有成功的HttpURLConnection 与 struts2 通信的例子。