HTTP POST 发送文件时收不到返回.
我用HttpURLConnection来模拟HTTP的POST请求,并在InputStream里面,发送文件.
本来想先描述问题的.可是模述不清,先上代码
发送的代码:
public static String postByte() {
byte[] param = ZhsqTestHttp.readFileByBytes("D:\\logs\\tmsInfoLog.log");
URL url = null;
HttpURLConnection httpURLConnection = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(POST_URL);
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
// 设置请求属性
httpURLConnection.setRequestProperty("Content-length", "" + param.length);
httpURLConnection.setRequestProperty("Content-Type", "application/octet-stream");
httpURLConnection.setRequestProperty("ID", "48XYHA8OVG5LNGAY9RRH97A6ESL9LNL5");
// 建立输出流,并写入数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(param);
outputStream.flush();
outputStream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
String line = "";
while ((line = reader.readLine()) != null) {
//返回打印处
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
sb.append("0");
} finally {
if (httpURLConnection != null)
httpURLConnection.disconnect();
}
System.out.println(sb.toString());
return sb.toString();
}
public String answer(String ID,HttpServletRequest request)throws ParamException {
ResultProxy result = new ResultProxy();
result.setCodeAndMsg(MsgCodeInfo.SUSS);
if (StringHelper.isNullOrEmpty(ID)) {
result.setCodeAndMsg(MsgCodeInfo.PARA_NULL_OR_ERROR);
result.setMsg("actionID 或 status,"+result.getMsg());
return JSON.toJSONString(result);
}else{
this.writeLogFile(request,answerFile);
return "OK";
}
}
public String writeLogFile(HttpServletRequest request,String fileMd5){
try {
String fileName = DateHelper.getDateNumByNow() + DateHelper.getUUID()+".log";
String filepath = createDir();
InputStream inputStream = request.getInputStream();
FileOutputStream fos = new FileOutputStream(filepath+"/"+fileName);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
return fileName;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
HTTP:输出方法:
public void outJSON(String str) {
//上线的时候,以JSON的方式进行传输.
try {
Object requestUUID = getRequest().getAttribute(MsgCodeInfo.PARA_REQUEST_UUID);
getResponse().setHeader("Pragma", "no-cache");
getResponse().setHeader("Cache-Control", "no-cache");
getResponse().setDateHeader("Expires", 0);
getResponse().setContentType("text/javascript;charset=utf-8");
this.addResponseLog(str, requestUUID.toString());//返回日志
} finally {
outSTR(str);
}
}
private final void outSTR(String str) {
PrintWriter out = null;
try {
out = getResponse().getWriter();{
out.write(str);
}
} catch (IOException e) {
logger.error("输出异常", e);
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
return "OK";
}
3.当内容较大的时候,
if (StringHelper.isNullOrEmpty(ID)) { //这里true的话.终段收不收到.(内容较小的时候能收到)
result.setCodeAndMsg(MsgCodeInfo.PARA_NULL_OR_ERROR);
result.setMsg("actionID 或 status,"+result.getMsg()); //所以觉得与内容无关.
return JSON.toJSONString(result);
}else{ //不走这里,与这里无关.
this.writeLogFile(request,answerFile);
return "OK";
}
3.上传内容在>95K时候, 服务端是执行到哪里?
-->从日志上看.
private final void outSTR(String str) {
PrintWriter out = null;
try {
out = getResponse().getWriter();{
out.write(str);
}
System.out.println("这一行,都已被打印出来了")
} catch (IOException e) {
logger.error("输出异常", e);
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
[其他解释]
private final void outSTR(String str,HttpServletResponse response,HttpServletRequest request) {
this.cleanStream(request);
PrintWriter out = null;
try {
out = response.getWriter();{
out.write(str);
}
} catch (IOException e) {
logger.error("输出异常", e);
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
public void cleanStream(HttpServletRequest request){
byte[] buffer = new byte[1024];
InputStream inputStream = null;
try {
inputStream = request.getInputStream();
while (inputStream.read(buffer) > 0) {
}
} catch (IOException e) {
e.printStackTrace();
return ;
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
return ;
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
postByte();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String postByte() throws IOException {
FileInputStream fis = new FileInputStream(new File("test.txt"));
byte[] param = new byte[300*1024];
fis.read(param);
URL url = null;
HttpURLConnection httpURLConnection = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL("http://127.0.0.1:8080/WebTest/MyServlet");
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
// 设置请求属性
httpURLConnection.setRequestProperty("Content-length", ""
+ param.length);
httpURLConnection.setRequestProperty("Content-Type",
"application/octet-stream");
httpURLConnection.setRequestProperty("ID",
"48XYHA8OVG5LNGAY9RRH97A6ESL9LNL5");
// 建立输出流,并写入数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(param);
outputStream.flush();
outputStream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
String line = "";
while ((line = reader.readLine()) != null) {
// 返回打印处
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
sb.append("0");
} finally {
if (httpURLConnection != null)
httpURLConnection.disconnect();
}
System.out.println(sb.toString());
return sb.toString();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
postByte();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String postByte() throws IOException {
FileInputStream fis = new FileInputStream(new File("test.txt"));
byte[] param = new byte[300*1024];
fis.read(param);
URL url = null;
HttpURLConnection httpURLConnection = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL("http://127.0.0.1:8080/WebTest/MyServlet");
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
// 设置请求属性
httpURLConnection.setRequestProperty("Content-length", ""
+ param.length);
httpURLConnection.setRequestProperty("Content-Type",
"application/octet-stream");
httpURLConnection.setRequestProperty("ID",
"48XYHA8OVG5LNGAY9RRH97A6ESL9LNL5");
// 建立输出流,并写入数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(param);
outputStream.flush();
outputStream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
String line = "";
while ((line = reader.readLine()) != null) {
// 返回打印处
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
sb.append("0");
} finally {
if (httpURLConnection != null)
httpURLConnection.disconnect();
}
System.out.println(sb.toString());
return sb.toString();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MyServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet");
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
writeLogFile(request);
out.writeChars("OK!");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
public void writeLogFile(HttpServletRequest request){
try {
InputStream inputStream = request.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("c:\\testaaa.log"));
byte[] buffer = new byte[1024];
while (inputStream.read(buffer) > 0) {
fos.write(buffer);
}
fos.flush();
fos.close();
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
// writeLogFile(request); 不读直接返回
out.writeChars("OK!");
out.flush();
out.close();