永发信息网

java获取服务器文件,怎样用url返回

答案:4  悬赏:40  手机版
解决时间 2021-04-05 06:41
  • 提问者网友:温柔港
  • 2021-04-04 09:18
java获取服务器文件,怎样用url返回
最佳答案
  • 五星知识达人网友:白昼之月
  • 2021-04-04 10:03
第一种; response.setStatus(302);
response.setHeader("location", "/dayX/MyHtml.html"); 该方式可以重定向到服务器指定页面
当然还有以下方式:
第二种;请求转发
请求转发是指将请求再转发到另一资源(一般为JSP或Servlet)。此过程依然在同一个请求范围内,转发后浏览器地址栏内容不变
请求转发使用RequestDispatcher接口中的forward()方法来实现,该方法可以把请求转发到另外一个资源,并让该资源对浏览器的请求进行响应request.getRequestDispatcher(path) .forward(request,response);
第三种 重定向
重定向是指页面重新定位到某个新地址,之前的请求失效,进入一个新的请求,且跳转后浏览器地址栏内容将变为新的指定地址
重定向是通过HttpServletResponse对象的sendRedirect()来实现,该方法相当于浏览器重新发送一个请求
response.sendRedirect(path);
全部回答
  • 1楼网友:底特律间谍
  • 2021-04-04 11:37
下面提供二种方法会使用java发送url请求,并获取服务器返回的值 第一种方法: 代码如下: import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.apache.http.params.coreconnectionpnames; import org.apache.http.util.entityutils; publicstaticstringsendurlrequest(stringurlstr,stringparam1,stringparam2)throwsexception{ stringtempstr=null; httpclienthttpclient=newdefaulthttpclient(); propertiesproperties=newproperties(); httpentityentity=null; stringxmlcontent=""; try { //设置超时时间 httpclient.getparams().setintparameter(coreconnectionpnames.connection_timeout,20000); httpclient.getparams().setparameter(coreconnectionpnames.so_timeout,20000); //封装需要传递的参数 listnvps=newarraylist(); nvps.add(newbasicnamevaluepair("mainmemocode",strmainmemocode)); nvps.add(newbasicnamevaluepair("recordpassword",strrecordpassword)); //客户端的请求方法类型 httpposthttppost=newhttppost(urlstr); httppost.setentity(newurlencodedformentity(nvps,"gbk")); httpresponseresponse=httpclient.execute(httppost); //获取服务器返回http的content-type的值 tempstr=response.getheaders("content-type")[0].getvalue().tostring(); //获取服务器返回页面的值 entity=response.getentity(); xmlcontent=entityutils.tostring(entity); stringstrmessage=null; system.out.println(xmlcontent); system.out.println(response.getheaders("content-type")[0].getvalue().tostring()); httppost.abort(); } catch(sockettimeoutexceptione) { } catch(exceptionex) { ex.printstacktrace(); } finally{ httpclient.getconnectionmanager().shutdown(); } 第二种方法: 代码如下: publicstaticstringsendurlrequest(stringurlstr,stringparam1,stringparam2)throwsexception{ httpurlconnectionurl_con=null; try{ urlurl=newurl(urlstr); stringbufferbankxmlbuffer=newstringbuffer(); //创建url连接,提交到数据,获取返回结果 httpurlconnectionconnection=(httpurlconnection)url.openconnection(); connection.setrequestmethod("post"); connection.setdooutput(true); connection.setrequestproperty("user-agent","directclient"); printwriterout=newprintwriter(newoutputstreamwriter(connection.getoutputstream(),"gbk")); out.println(param); out.close(); bufferedreaderin=newbufferedreader(newinputstreamreader(connection .getinputstream(),"gbk")); stringinputline; while((inputline=in.readline())!=null){ bankxmlbuffer.append(inputline); } in.close(); tempstr=bankxmlbuffer.tostring(); } catch(exceptione) { system.out.println("发送get请求出现异常!"+e); e.printstacktrace(); }finally{ if(url_con!=null) url_con.disconnect(); } returntmpestr; } 总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。
  • 2楼网友:洒脱疯子
  • 2021-04-04 10:27
那不就是下载文件吗
  • 3楼网友:行路难
  • 2021-04-04 10:16
写JSP或 servlet,根据URL去读文件 ~ ~ ~ ~
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯