Android携帯クライアントはファイルをアップロードし、java servletサーバ側はサーバに受信して保存する


内容は問題のようで、多く言わないで、直接コードに行きます.
Androidクライアントコード:
public class MainActivity extends Activity
{
	private TextView uploadInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		uploadInfo = (TextView) findViewById(R.id.upload_info);

		uploadFile();
	}

	public void uploadFile()
	{
		//      
		String url = "http://192.168.0.108:8080/UploadFileServer/upload";
		//         ,              
		String filePath = Environment.getExternalStorageDirectory()
				+ "/1/power.apk";

		AsyncHttpClient httpClient = new AsyncHttpClient();

		RequestParams param = new RequestParams();
		try
		{
			param.put("file", new File(filePath));
			param.put("content", "liucanwen");
			
			httpClient.post(url, param, new AsyncHttpResponseHandler()
			{
				@Override
				public void onStart()
				{
					super.onStart();
					
					uploadInfo.setText("    ...");
				}
				
				@Override
				public void onSuccess(String arg0)
				{
					super.onSuccess(arg0);

					Log.i("ck", "success>" + arg0);
					
					if(arg0.equals("success"))
					{
						Toast.makeText(MainActivity.this, "    !", 1000).show();
					}
					
					uploadInfo.setText(arg0);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1)
				{
					super.onFailure(arg0, arg1);
					
					uploadInfo.setText("    !");
				}
			});
			
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			Toast.makeText(MainActivity.this, "       !", 1000).show();
		}
	}
}

サーバ側コード:
public class UploadFileServlet extends HttpServlet
{

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		//           
		DiskFileItemFactory factory = new DiskFileItemFactory();

		//         
		String upload = this.getServletContext().getRealPath("/upload/");
		//                ,    Tomcat     temp   
		String temp = System.getProperty("java.io.tmpdir");
		//          5M
		factory.setSizeThreshold(1024 * 1024 * 5);
		//         temp
		factory.setRepository(new File(temp));
		//           ,ServletFileUpload           
		ServletFileUpload servletFileUpload = new ServletFileUpload(factory);

		//       List 
		try
		{
			List<FileItem> list = servletFileUpload.parseRequest(request);

			for (FileItem item : list)
			{
				String name = item.getFieldName();
				InputStream is = item.getInputStream();

				if (name.contains("content"))
				{
					System.out.println(inputStream2String(is));
				} else if(name.contains("file"))
				{
					try
					{
						inputStream2File(is, upload + "\\" + item.getName());
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
			
			out.write("success");
		} catch (FileUploadException e)
		{
			e.printStackTrace();
			out.write("failure");
		}

		out.flush();
		out.close();
	}

	//        
	public static String inputStream2String(InputStream is) throws IOException
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = is.read()) != -1)
		{
			baos.write(i);
		}
		return baos.toString();
	}

	//       
	public static void inputStream2File(InputStream is, String savePath)
			throws Exception
	{
		System.out.println("       :" + savePath);
		File file = new File(savePath);
		InputStream inputSteam = is;
		BufferedInputStream fis = new BufferedInputStream(inputSteam);
		FileOutputStream fos = new FileOutputStream(file);
		int f;
		while ((f = fis.read()) != -1)
		{
			fos.write(f);
		}
		fos.flush();
		fos.close();
		fis.close();
		inputSteam.close();
		
	}

}

クライアントとサーバ側コードのダウンロードアドレス:
http://download.csdn.net/detail/qq15989177612/6900727