问题描述
感觉崩溃模仿别人写的··改改··好像有点问题···大体上功能是post请求给servlet,我转发给另一个servlet··解析返回值,取其中一部分,然后传给另一个jsp页面,输出ShuJuservice.javapublicclassShuJuServletextendsHttpServlet{@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//TODOAuto-generatedmethodstubreq.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");StringrespMessage=ShuJuService.processRequest(req);req.setAttribute("result",respMessage);req.getRequestDispatcher("2.jsp").forward(req,resp);}}ShuJuservier,javapublicclassShuJuService{publicstaticStringprocessRequest(HttpServletRequestreq)throwsIOException{ServletInputStreamsis=req.getInputStream();finalintBUFFER_SIZE=8*1024;byte[]buffer=newbyte[BUFFER_SIZE];intlength=0;ByteArrayOutputStreambaos=newByteArrayOutputStream();intbLen=0;while((bLen=sis.read(buffer))>0){baos.write(buffer,0,bLen);}Stringbao=baos.toString();Stringjson=formUpload("http://121.42.43.44:80/bar/serv/pushAttachment",bao);ShuJuResultShuJuResult=newGson().fromJson(json,ShuJuResult.class);Stringresult=ShuJuResult.getResult();StringresultUrl="http://121.42.43.44:80/bar/sample/preview?aid={aid}";resultUrl=resultUrl.replace("{aid}",result);returnresultUrl;}publicstaticStringformUpload(StringurlStr,Stringreq){Stringres="";HttpURLConnectionconn=null;StringBOUNDARY="---------------------------123821742118716";//boundary就是request头和上传文件内容的分隔符try{URLurl=newURL(urlStr);conn=(HttpURLConnection)url.openConnection();conn.setConnectTimeout(5000);conn.setReadTimeout(30000);conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setRequestMethod("POST");conn.setRequestProperty("Connection","Keep-Alive");conn.setRequestProperty("User-Agent","Mozilla/5.0(Windows;U;WindowsNT6.1;zh-CN;rv:1.9.2.6)");conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+BOUNDARY);OutputStreamout=newDataOutputStream(conn.getOutputStream());//textout.write(req.toString().getBytes());out.flush();out.close();//读取返回数据StringBufferstrBuf=newStringBuffer();BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));//BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream(),"utf-8"));Stringline=null;while((line=reader.readLine())!=null){strBuf.append(line).append("n");}res=strBuf.toString();reader.close();reader=null;}catch(Exceptione){System.out.println("发送POST请求出错。"+urlStr);e.printStackTrace();}finally{if(conn!=null){conn.disconnect();conn=null;}}returnres;}}
解决方案
解决方案二:
代码不完整。
解决方案三:
············哪块不完整······你是说没有最外面的头············
解决方案四:
好吧··完整的代码···ShuJuService.javapackageorg.wangzizhao.course.service;importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;importjava.text.SimpleDateFormat;importjava.util.Iterator;importjava.util.Map;importjavax.servlet.ServletInputStream;importjavax.servlet.http.HttpServletRequest;importcom.google.gson.Gson;publicclassShuJuService{publicstaticStringprocessRequest(HttpServletRequestreq)throwsIOException{ServletInputStreamsis=req.getInputStream();finalintBUFFER_SIZE=8*1024;byte[]buffer=newbyte[BUFFER_SIZE];intlength=0;ByteArrayOutputStreambaos=newByteArrayOutputStream();intbLen=0;while((bLen=sis.read(buffer))>0){baos.write(buffer,0,bLen);}Stringbao=baos.toString();Stringjson=formUpload("http://121.42.43.44:80/bar/serv/pushAttachment",bao);ShuJuResultShuJuResult=newGson().fromJson(json,ShuJuResult.class);Stringresult=ShuJuResult.getResult();StringresultUrl="http://121.42.43.44:80/bar/sample/preview?aid={aid}";resultUrl=resultUrl.replace("{aid}",result);returnresultUrl;}publicstaticStringformUpload(StringurlStr,Stringreq){Stringres="";HttpURLConnectionconn=null;StringBOUNDARY="---------------------------123821742118716";//boundary就是request头和上传文件内容的分隔符try{URLurl=newURL(urlStr);conn=(HttpURLConnection)url.openConnection();conn.setConnectTimeout(5000);conn.setReadTimeout(30000);conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setRequestMethod("POST");conn.setRequestProperty("Connection","Keep-Alive");conn.setRequestProperty("User-Agent","Mozilla/5.0(Windows;U;WindowsNT6.1;zh-CN;rv:1.9.2.6)");conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+BOUNDARY);OutputStreamout=newDataOutputStream(conn.getOutputStream());//textout.write(req.toString().getBytes());out.flush();out.close();//读取返回数据StringBufferstrBuf=newStringBuffer();BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));//BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream(),"utf-8"));Stringline=null;while((line=reader.readLine())!=null){strBuf.append(line).append("n");}res=strBuf.toString();reader.close();reader=null;}catch(Exceptione){System.out.println("发送POST请求出错。"+urlStr);e.printStackTrace();}finally{if(conn!=null){conn.disconnect();conn=null;}}returnres;}}ShuJuServlet.javapackageorg.wangzizhao.course.servlet;importjava.io.BufferedReader;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.Iterator;importjava.util.Map;importjavax.activation.MimetypesFileTypeMap;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.wangzizhao.course.service.ShuJuService;publicclassShuJuServletextendsHttpServlet{@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//TODOAuto-generatedmethodstubreq.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");StringrespMessage=ShuJuService.processRequest(req);req.setAttribute("result",respMessage);req.getRequestDispatcher("2.jsp").forward(req,resp);}}ShuJuResult.javapackageorg.wangzizhao.course.service;publicclassShuJuResult{Stringerror;Stringresult;publicStringgetError(){returnerror;}publicvoidsetError(Stringerror){this.error=error;}publicStringgetResult(){returnresult;}publicvoidsetResult(Stringresult){this.result=result;}}接受页面<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%><%Stringresult=(String)request.getAttribute("result");%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!--<linkrel="stylesheet"type="text/css"href="styles.css">--></head><body>文件预览链接:<%=result%><br></body></html>post请求页面<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=GBK"/><title>无标题文档</title></head><body><formrole="form"action="http://121.42.43.44:80/weixin/ShuJuServlet"method="post"enctype="multipart/form-data">文件上传:<inputtype="file"name="file">deadline<inputtype="text"name="deadline"value="2014-10-1023:00:00"><inputtype="submit"value="上传"></form></body></html>