服务器端用myeclipse写的,每次调试logcat无任何错误,每一行代码都顺利执行,可是在服务器端就是看不到照片也没有任何反应这是为什么啊
服务器端:
package com.image.upload;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadAction extends HttpServlet {
public UploadAction() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("图片上传路径-------》");
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getContextPath();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload();
fileUpload.setSizeMax(10 * 1024 * 1024);
fileUpload.setFileSizeMax(10 * 1024 * 1024);
List<FileItem> list = null;
System.out.println("图片上传路径-------》");
try {
list = fileUpload.parseRequest(request);
for (FileItem item : list) {
if(!item.isFormField())
{
String imageString = item.getName();
String file_upload_path=request.getRealPath("/upload")+"/"+imageString;
System.out.println("图片上传路径-------》"+file_upload_path);
item.write(new File(file_upload_path));
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void init() throws ServletException {
// Put your code here
}
}
手机端
main activity:
package com.example.android_camera_upload;
import java.io.ByteArrayOutputStream;
import org.apache.http.util.ByteArrayBuffer;
import com.example.android_camera_upload.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private ImageView imageview1;
private byte[] image_data; // 存儲圖片的數據
private final String upload_pathString="http://166.111.82.103:8080/uploadImage/UploadAction";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
imageview1 = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1000);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("调试信息","执行post");
HttpPostUtils.sendFormByPost(upload_pathString, image_data, "aa.png");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bm = (Bitmap) bundle.get("data");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 50, outputStream);
image_data = outputStream.toByteArray();
imageview1.setImageBitmap(bm);
}
}
}
工具类HttpPostUtils
package com.example.android_camera_upload;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class HttpPostUtils {
public static void sendFormByPost(final String path,
final byte[] fileBody, final String fileName) {
boolean flag = false;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpClient httpClient=new DefaultHttpClient();
Log.i("調試信息","111");
HttpPost httpPost=new HttpPost(path);
Log.i("調試信息","2");
MultipartEntity entity=new MultipartEntity();
Log.i("調試信息","3");
entity.addPart("myfile",new ByteArrayBody(fileBody, fileName));
Log.i("調試信息","4");
httpPost.setEntity(entity);
Log.i("調試信息","5");
httpClient.execute(httpPost);
Log.i("調試信息","6");
} catch (Exception e) {
Log.i("調試信息","错误");
e.printStackTrace();
// TODO: handle exception
}
}
}).start();
// return flag;
}
}
所有的调试信息都出现而且也不报任何错误但是服务器端就是没有反应,没有报错,这是为什么!!
我猜是你手机端与服务器连接之间的问题..你在配置文件中将localhost之类的换成服务器的具体IP试试