解决如何从服务器上下载图片或文件到本地的方案代码

解决如何从服务器上下载图片或文件到本地的方案代码

1.前台html的代码

 
    <label class="layui-form-label mingzi">下载内容</label>
   // 需要下载的内容名称填写的文本框
  <input type="text" name="xiazaiId"  class="layui-input kuan">
    //点击连接
  <button type="button" class="layui-btn xia" >立即下载</button>
  

2.前台js的代码

 $(document).on("click",".xia",function(){
			var xiazaiId= $("input[name='xiazaiId']").val();
			location.href="${pageContext.request.contextPath }/addxiazai.json?xiazaiId="+xiazaiId;
			
		 }) 

3.后台相应的代码

       @ResponseBody
	   @RequestMapping(value="/addxiazai")
		public void addEvaluate(HttpServletRequest request,HttpServletResponse response)				
		{ 
		   String xiazaiId=request.getParameter("xiazaiId");
		   System.out.println("xiazai========="+xiazaiId);
		   xiazai.download0(xiazaiId,"GET",response);
	
		}


 public static void download0(String  fileName, String method,HttpServletResponse response) {
      //  System.out.println("下载文件路径:" + path);
    	 String contextPath =  "http://www.pwxvr.com/dangjian/statics/uploadfiles1/"+fileName;
        System.out.println("contextPath======"+contextPath);
     
        InputStream inputStream = null;
        OutputStream bufferOut = null;
   
        
        HttpURLConnection conn = null;
        try {
        	
        	 URL url = new URL(contextPath);
             // 连接类的父类,抽象类
             URLConnection urlConnection = url.openConnection();
             // http的连接类
             HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
             //设置超时      
             httpURLConnection.setConnectTimeout(1000*5);
             //设置请求方式,默认是GET
             httpURLConnection.setRequestMethod("GET");
             // 设置字符编码
             httpURLConnection.setRequestProperty("Charset", "UTF-8");
             httpURLConnection.connect();
             int contentLength = httpURLConnection.getContentLength();
             inputStream=httpURLConnection.getInputStream();
             
             
          //获取文件名
	     String fileFullName = contextPath.substring(contextPath.lastIndexOf("/")).substring(1);
        
        
            // 设置响应报头
	        response.reset();//重置 响应头
            response.setContentLength(contentLength);
            response.setContentType("application/x-jpg");
            response.addHeader("Content-Disposition", "attachment; filename=" +fileFullName);
           
            bufferOut = response.getOutputStream();
        
            byte[] buffer = new byte[1024];
            int len=0;
            while ((len=inputStream.read(buffer))!=-1){
                bufferOut.write(buffer, 0, len);
            }
           // bufferOut.flush();
            System.out.println("下载成功啦————————————————————————————————");
        } 
        catch (Throwable e) {
			  if (e instanceof ClientAbortException) { // 浏览器点击取消
			  System.out.println("用户取消下载!");
			  } else {
			   System.out.println("下载文件失败....!");
			  } 
            e.printStackTrace();
        } finally {
		            try {
		                if (bufferOut != null) {
		                    bufferOut.close();
		                }
		                if (inputStream != null) {
		                    inputStream.close();
		                }
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
        }
    }
  

4.谢谢各位的浏览

     工作几年,略有心得,然很少整理成文,平常工作中浏览了不少道友的好文章,受益良多。
      顾也开始写一些文章, 希望能与各位道友分享进步 !!!!
     如果有用,欢迎各位点赞评论,若有不足,亦可指正!!!
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曦月合一

你的鼓励是我们前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值