在STRUTS中如何通过request获取从HttpURLConnection写出的流对象
2012-11-05 10:52
489 查看
最近在一个项目中,需要从HttpURLConnection中写出流,在STRUTS中通过request获取流对象,但是,不管怎么样操作,在STRUTS的request中就是不能获取对应的流,很郁闷的说,之后找到了关键点,因为流写出的时候设置了表单提交的形式,导致STRUTS中获取流时出现了问题,struts对没有指定content-type的request请求,封装时候作了一些处理,导致无法在Action中获取request.getInputStream() 和 request.getReader()。
详细可以查看代码例子。
1. sendPost方法,从本地中获取流,写入到相应的url链接中:
(重点):
//需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数
conn.setRequestProperty("content-type", "text/html"); //直接传递流对象
//以下的则是通过form组件的形式来传递流对象的,具体使用上网查看。
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
[java]
view plaincopyprint?
public
static void main(String[] args)
throws UnsupportedEncodingException {
// String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011";
String url = "http://localhost:8080/webtest/servlet/URLTest?name=linlin";
// String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg";
// getReturnData1(url);
sendPost(url,null);
}
[java]
view plaincopyprint?
/**
* 通过HTTP协议以POST形式发送指定文件至指定url
* @param url
* @throws IOException
*/
public static
void sendPost(String url,InputStream in) {
HttpURLConnection conn = null;
OutputStreamWriter osw = null;
try {
File file = new File("D:/test2.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url1 = new URL(url);
conn = (HttpURLConnection)url1.openConnection();
conn.setReadTimeout(10000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false);
// 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Charsert",
"UTF-8");
//conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
//需要传递流时,一定要添加的内容,而且ACTION中通过request.getInputStream获取也必须添加该选项
conn.setRequestProperty("content-type",
"text/html");
OutputStream o = conn.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int BUFFER_SIZE = 1024;
byte[] buf =
new byte[BUFFER_SIZE];
int size = 0;
try {
while ((size = bis.read(buf)) != -1)
o.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
System.out.println( "connect failed!");
} catch (IOException e) {
e.printStackTrace();
}
finally
{
if (osw != null)
try {
osw.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
if (conn !=
null)
conn.disconnect() ;
}
}
2. 当按照以上方法写出流时,就可以在servlet或者action中获取对应的流信息了,代码如下:
[java]
view plaincopyprint?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String s = request.getParameter("name");
System.out.println("s22 is " + s);
InputStream in = request.getInputStream();
if(in != null) {
System.out.println("流不是空的。");
this.writeInputStreamToFile(in);
System.out.println("server time is " +
new Date());
} else {
System.out.println("流是空的。");
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
[java]
view plaincopyprint?
private void writeInputStreamToFile(InputStream in)
throws FileNotFoundException {
File file = new File("D:/test3.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
int BUFFER_SIZE =
1024;
byte[] buf = new
byte[BUFFER_SIZE];
int size =
0;
try {
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
详细可以查看代码例子。
1. sendPost方法,从本地中获取流,写入到相应的url链接中:
(重点):
//需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数
conn.setRequestProperty("content-type", "text/html"); //直接传递流对象
//以下的则是通过form组件的形式来传递流对象的,具体使用上网查看。
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
[java]
view plaincopyprint?
public
static void main(String[] args)
throws UnsupportedEncodingException {
// String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011";
String url = "http://localhost:8080/webtest/servlet/URLTest?name=linlin";
// String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg";
// getReturnData1(url);
sendPost(url,null);
}
public static void main(String[] args) throws UnsupportedEncodingException { // String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011"; String url = "http://localhost:8080/webtest/servlet/URLTest?name=linlin"; // String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg"; // getReturnData1(url); sendPost(url,null); }
[java]
view plaincopyprint?
/**
* 通过HTTP协议以POST形式发送指定文件至指定url
* @param url
* @throws IOException
*/
public static
void sendPost(String url,InputStream in) {
HttpURLConnection conn = null;
OutputStreamWriter osw = null;
try {
File file = new File("D:/test2.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url1 = new URL(url);
conn = (HttpURLConnection)url1.openConnection();
conn.setReadTimeout(10000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false);
// 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Charsert",
"UTF-8");
//conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
//需要传递流时,一定要添加的内容,而且ACTION中通过request.getInputStream获取也必须添加该选项
conn.setRequestProperty("content-type",
"text/html");
OutputStream o = conn.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int BUFFER_SIZE = 1024;
byte[] buf =
new byte[BUFFER_SIZE];
int size = 0;
try {
while ((size = bis.read(buf)) != -1)
o.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
System.out.println( "connect failed!");
} catch (IOException e) {
e.printStackTrace();
}
finally
{
if (osw != null)
try {
osw.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
if (conn !=
null)
conn.disconnect() ;
}
}
/** * 通过HTTP协议以POST形式发送指定文件至指定url * @param url * @throws IOException */ public static void sendPost(String url,InputStream in) { HttpURLConnection conn = null; OutputStreamWriter osw = null; try { File file = new File("D:/test2.jpg"); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } URL url1 = new URL(url); conn = (HttpURLConnection)url1.openConnection(); conn.setReadTimeout(10000); // 缓存的最长时间 conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); conn.setRequestProperty("Charsert", "UTF-8"); //conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString()); //需要传递流时,一定要添加的内容,而且ACTION中通过request.getInputStream获取也必须添加该选项 conn.setRequestProperty("content-type", "text/html"); OutputStream o = conn.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int BUFFER_SIZE = 1024; byte[] buf = new byte[BUFFER_SIZE]; int size = 0; try { while ((size = bis.read(buf)) != -1) o.write(buf, 0, size); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); o.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) System.out.println( "connect failed!"); } catch (IOException e) { e.printStackTrace(); } finally { if (osw != null) try { osw.close() ; } catch (IOException e1) { e1.printStackTrace(); } if (conn != null) conn.disconnect() ; } }
2. 当按照以上方法写出流时,就可以在servlet或者action中获取对应的流信息了,代码如下:
[java]
view plaincopyprint?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String s = request.getParameter("name");
System.out.println("s22 is " + s);
InputStream in = request.getInputStream();
if(in != null) {
System.out.println("流不是空的。");
this.writeInputStreamToFile(in);
System.out.println("server time is " +
new Date());
} else {
System.out.println("流是空的。");
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String s = request.getParameter("name"); System.out.println("s22 is " + s); InputStream in = request.getInputStream(); if(in != null) { System.out.println("流不是空的。"); this.writeInputStreamToFile(in); System.out.println("server time is " + new Date()); } else { System.out.println("流是空的。"); } PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
[java]
view plaincopyprint?
private void writeInputStreamToFile(InputStream in)
throws FileNotFoundException {
File file = new File("D:/test3.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
int BUFFER_SIZE =
1024;
byte[] buf = new
byte[BUFFER_SIZE];
int size =
0;
try {
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- 在STRUTS中如何通过request获取从HttpURLConnection写出的流对象
- 在portlet开发中,如何使用RenderRequest对象获取HttpServlertRequest中的值?求解答
- 如何通过HttpURLConnection得到http 302的跳转地址
- 找不到好的方法,如何通过HttpWebRequest获取页面的Encoding。(转)
- Android通过HttpURLConnection获取JSON并进行UI更新
- Android通过HttpURLConnection链接到网络,并获取网络数据
- 通过HttpServletRequest对象获取信息
- 后台通过HttpServletRequest对象获取页面表单传递过来的参数的方式
- 基于jersey的pojo对象如何获取HttpServletRequest对象
- 如何通过HttpRequest对象获取路径和URL片段?
- 通过HttpURLConnection连接服务器,发送报文,获取服务器报文返回
- 如何通过HttpURLConnection得到http 302的跳转地址
- 黄聪:Greasemonkey研究,通过GM_xmlhttpRequest获取远程URL的内容
- HttpURLConnection对象的获取
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 通过HttpURLConnection获取网络图片实例
- Action中获取HttpServletRequest对象的方法(表单提交值,url传递参数)
- 通过URLHttpConnection方式连接网络步骤,获取位图为例
- 如何通过HttpURLConnection得到http 302的跳转地址
- JSF(Java Server Face) 框架中如何获取传统的HttpSession或HttpServletRequest对象