Android使用AsyncTask加http用post方式上传图片到服务器

android端代码展示:


 /* 上传文件至Server的方法 */
    private void uploadFile() {
        System.out.print("正在发送请求!");
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {

            String urlpost=actionUrl;
            URL url = new URL(urlpost);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
			/* 允许Input、Output,不使用Cache */
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
			/* 设置传送的method=POST */
            con.setRequestMethod("POST");
			/* setRequestProperty */
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
			/* 设置DataOutputStream */
            DataOutputStream ds = new DataOutputStream(con.getOutputStream());
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; "
                    + "name=\"file1\";filename=\"" + newName + "\"" + end);


            ds.writeBytes(end);
			/* 取得文件的FileInputStream */
            FileInputStream fStream = new FileInputStream(uploadFile);
			/* 设置每次写入1024bytes */
            System.out.print("已经找到数据正在发送!");
            int bufferSize = 1024*10;
            byte[] buffer = new byte[bufferSize];
            int length = -1;
			/* 从文件读取数据至缓冲区 */
            while ((length = fStream.read(buffer)) != -1) {
				/* 将资料写入DataOutputStream中 */
                ds.write(buffer, 0, length);
            }
            ds.writeBytes(end);
            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

			/* close streams */
            fStream.close();
            ds.flush();
			/* 取得Response内容 */
            InputStream is = con.getInputStream();
            int ch;
            StringBuffer b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
			/* 将Response显示于Dialog */

			/* 关闭DataOutputStream */
            ds.close();
            try {
                InfoUrl=InfoUrl+"?username="+username.getText()+"&password="+password.getText()+"&img="+b.toString();

                Log.i("login", "uploadFile: "+InfoUrl);
                HttpURLConnection huc= (HttpURLConnection) new URL(InfoUrl).openConnection();
                huc.setDoInput(true);
                huc.setDoOutput(true);
                huc.setUseCaches(false);
			/* 设置传送的method=POST */
                huc.setRequestMethod("GET");
                huc.connect();
                InputStream is1 = huc.getInputStream();
                int ch1;
                StringBuffer b1 = new StringBuffer();
                while ((ch1 = is1.read()) != -1) {
                    b1.append((char) ch1);
                }
                int result=Integer.parseInt(b1.toString());

                if(result>0) {
                    showDialog("注册成功,请登录!");
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);

                }else {
                    showDialog("注册失败,请检查!");
                }



            } catch (Exception e) {
                e.printStackTrace();
            }


        } catch (Exception e) {
            System.out.print("网络出现异常!");
            showDialog("上传失败");
            e.printStackTrace();
        }
    }

服务器端:
PrintWriter out = response.getWriter();
		
		FileUploadTool fut=new FileUploadTool(request,"photo");
		
		String name=fut.getParameter("file1");
		
		
		out.print(name);

猜你喜欢

转载自blog.csdn.net/qq_28695593/article/details/53068945