文件传输

public static int getRecivedDate(HttpServletRequest request, String fileName)
    {
        log.debug("***********************start run MPPdisposalFile.getRecivedDate() method: ");
        StringBuffer recieveData = new StringBuffer(1024);
        String inputLine = null;
        BufferedReader in = null;
        try
        {
            in = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            File file = new File(fileName);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buf;
            while ((inputLine = in.readLine()) != null)
            {
                buf = inputLine.getBytes();
                buf = Base64.decode(inputLine);
                fos.write(buf);
                recieveData.append(inputLine);
            }
            fos.flush();
            fos.close();

            if (LOG.isDebugEnabled())
            {
                LOG.debug("received ,recieveData[" + recieveData.toString() + "]");
            }

            if (recieveData != null)
            {
                log
                    .debug("***********************end run MPPdisposalFile.getRecivedDate() method: result is success ");
                return SEND_SUCCESS;
            }
            else
            {
                log
                    .debug("***********************end run MPPdisposalFile.getRecivedDate() method: result is faile");
                return FAILE;
            }
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            LOG.error("Received data error", e);
            return FAILE;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            LOG.error("Received data error", e);
            return FAILE;
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                }
            }
        }
    }

public static boolean httpUpload(String url, String exsistingFileName) {
  log
    .debug("***********************start run PhoneNewsTask.httpUpload()method:");
  URLConnection urlCon = null;
  OutputStreamWriter out = null;
  BufferedReader reader = null;
  DataOutputStream dataOut = null;
  try {
   urlCon = (new URL(url)).openConnection();
   // 设置请求类型,防止请求的XML体被过滤
   urlCon
     .setRequestProperty("Content-Type",
       "text/xml; charset=UTF-8");
   urlCon.setRequestProperty("Accept",
     "application/x-www-form-urlencoded");
   urlCon.setRequestProperty("version", "100");
   urlCon.setDoOutput(true);
   urlCon.setDoInput(true);
   urlCon.setReadTimeout(60000);
   dataOut = new DataOutputStream(urlCon.getOutputStream());
   java.io.OutputStream raw = urlCon.getOutputStream();
   java.io.OutputStream buf = new BufferedOutputStream(raw);
   out = new OutputStreamWriter(buf, "UTF-8");
   FileInputStream fis = new FileInputStream(new File(
     exsistingFileName));
   int maxBufferSize = 1 * 1024 * 1024;
   int bytesAvailable = fis.available();
   int bufferSize = Math.min(bytesAvailable, maxBufferSize);
   byte[] b = new byte[bufferSize];
   fis.read(b);
   String encode = Base64.encode(b);
   out.write(encode);
   // log.debug("file content:" + new String(b));
   out.flush();
   out.close();
   fis.close();
   reader = new BufferedReader(new InputStreamReader(urlCon
     .getInputStream(), "UTF-8"));
   String line = null;
   StringBuffer xmlStr = new StringBuffer();
   boolean flag = true;
   while ((line = reader.readLine()) != null) {
    xmlStr.append(line);
    flag = true;
   }
   String xmlstr = xmlStr.toString().trim();

   if (log.isDebugEnabled()) {
    log.debug("xmlstr[" + xmlstr + "]");
   }
   log
     .debug("***********************end run PhoneNewsTask.httpUpload()method:");
   return flag;
  } catch (IOException e) {
   log.error("io Exception.", e);
   return false;
  } catch (Exception e) {
   log.error("Exception.", e);
   return false;
  } finally {
   try {
    if (dataOut != null) {
     dataOut.close();
     dataOut = null;
    }
   } catch (IOException e) {
    dataOut = null;
   }
   try {
    if (reader != null) {
     reader.close();

     reader = null;
    }
   } catch (IOException e) {
    reader = null;
   }

  }

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lvxiaohuai

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值