文件传输
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;
}
}
}
本文地址:https://blog.csdn.net/sxlvxh/article/details/5669148
上一篇: 6_1_sql_create
下一篇: Android NDK中的c++ STL