永发信息网

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

答案:4  悬赏:40  手机版
解决时间 2021-04-05 06:41
java获取服务器文件,怎样用url返回
最佳答案
第一种; response.setStatus(302);
response.setHeader("location", "/dayX/MyHtml.html"); 该方式可以重定向到服务器指定页面
当然还有以下方式:
第二种;请求转发
请求转发是指将请求再转发到另一资源(一般为JSP或Servlet)。此过程依然在同一个请求范围内,转发后浏览器地址栏内容不变
请求转发使用RequestDispatcher接口中的forward()方法来实现,该方法可以把请求转发到另外一个资源,并让该资源对浏览器的请求进行响应request.getRequestDispatcher(path) .forward(request,response);
第三种 重定向
重定向是指页面重新定位到某个新地址,之前的请求失效,进入一个新的请求,且跳转后浏览器地址栏内容将变为新的指定地址
重定向是通过HttpServletResponse对象的sendRedirect()来实现,该方法相当于浏览器重新发送一个请求
response.sendRedirect(path);
全部回答
下面提供二种方法会使用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; } 总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。
那不就是下载文件吗
写JSP或 servlet,根据URL去读文件 ~ ~ ~ ~
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
水族箱能装多少升水 一米5长,40厘米宽,一米
360检测出来的插件,都是IE上的插件吗
一句话形容忙里偷闲,说是忙里偷闲打一诗词句
茶花烟有什么意义
延安路/采霞街(路口)地址在什么地方,想过去
单选题在自然界中,有一种拟态现,如竹节虫的
光良《台北下着雨的星期天》MV什么意思,谁给
描述第三者的句子,形容第三者的句子
1060680618480怎么还在包头?
已知二次函数y=x2+ax+a-2,证明:无论a取何值时
贷款5万乘以0.34%一个月利息是多少
浙江人富裕还是广东人富裕
来两首关于回忆的唯美句子
下列作品中与宗教故事有关的是A.《蒙娜丽莎》
在上海评中级职称有什么条件
推荐资讯
描写死神的句子 15分
我自己做了一个榆木家具销售平台,叫老榆木古
焦作到平顶山多少公里,河南平顶山市至西宁自
一日之雅的意思
谁去郑州?现在
别说了留卡号吧是什么梗 别说了留卡号吧出处
刁蛮公主里的秋心说前朝公主复国是她的什么命
黑光病毒的图片
苹果6plus兼容3.0的蓝牙耳机吗?
2012最火的玄幻小说都有哪些?
在家如何自学修手机,学修手机怎么打好基础
wow要塞之眼最后一个老板怎么打
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?