android上传图片到服务器SSH搭建

这两天被上传图片煎熬着,最终还是做出来了

android上传图片

import java.io.File;
import java.io.FileNotFoundException;

import com.loopj.android.http.AsyncHttpClient;//一个开源的文件传输
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText filePathTxt = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		filePathTxt = (EditText) this.findViewById(R.id.filePathTxt);
	}
	
	public void upload(View v) throws FileNotFoundException{
	
		String url = "http://10.0.2.2:8080/Shop/uploadFile!upload.action";
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		File f = new File(filePathTxt.getText().toString());
		if(f.exists() && f.length() > 0){
			params.put("upload", f);
			params.put("name", "name111");
		}
		
		client.post(url, params, new AsyncHttpResponseHandler(){

			@Override
			public void onSuccess(String content) {
				// TODO Auto-generated method stub
				super.onSuccess(content);
				Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
			}
			
		});
		
	}
}
我们用到的这一部分主要是把需要传输的数据伪装成一个form的形式,向后台传输,具体讲解在此

http://blog.csdn.net/zhjp4295216/article/details/5534563

这让我更喜欢编程这一行业了,虽然还好多不会的,但是发现程序员都好聪明啊

web端接收



import java.io.File;
import java.io.IOException;
import java.util.List;

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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class ImageUploadActionPhone extends ActionSupport {
{
	String name;
	
	File upload;
	
	
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void upload(){
			   String s = "D:/workspace/MyEclipse/Shop/WebRoot/upload/";
				
				String targetDirectory =s;
				String targetFileName = "success.jpg";
				File target = new File(targetDirectory,targetFileName);

				try {
					FileUtils.copyFile(getUpload(), target);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
	}
}
在服务器端接收传过来的图片

按照前台是form那样接收就可以了

android上传图片到服务器SSH搭建,布布扣,bubuko.com

android上传图片到服务器SSH搭建

上一篇:chrome33下模拟手机显示页面(开启设备模拟器)


下一篇:iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上