android servre弹窗,图像未上传到php服务器,android上的servre没有响应

package com.telubi.connectivity;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import android.util.Log;

import com.cipl.TennisApp.Login;

public class FileUploader {

private String Tag = \"UPLOADER\";

private String urlString;// = \"YOUR_ONLINE_PHP\";

HttpURLConnection conn;

String exsistingFileName;

public String result;

public String uploadImageData(String serverImageTag) {// Server image tag

String lineEnd = \"\\r\\n\";

String twoHyphens = \"--\";

String boundary = \"*****\";

try {

// ------------------ CLIENT REQUEST

Log.e(Tag, \"Inside second Method\");

FileInputStream fileInputStream = new FileInputStream(new File(

exsistingFileName));

// open a URL connection to the Servlet

URL url = new URL(urlString);

// Open a HTTP connection to the URL

conn = (HttpURLConnection) url.openConnection();

// Allow Inputs

conn.setDoInput(true);

// Allow Outputs

conn.setDoOutput(true);

// Don\'t use a cached copy.

conn.setUseCaches(false);

// Use a post method.

conn.setRequestMethod(\"POST\");

conn.setRequestProperty(\"Connection\", \"Keep-Alive\");

conn.setRequestProperty(\"Content-Type\",

\"multipart/form-data;boundary=\" + boundary);

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);

if (serverImageTag.equalsIgnoreCase(\"courtImage\")) {

dos.writeBytes(\"Content-Disposition: post-data; name=courtImage[];filename=\"

+ exsistingFileName + \"\" + lineEnd);

} else if (serverImageTag.equalsIgnoreCase(\"userImage\")) {

dos.writeBytes(\"Content-Disposition: post-data; name=userImage[];filename=\"

+ exsistingFileName + \"\" + lineEnd);

}

dos.writeBytes(lineEnd);

Log.e(Tag, \"Headers are written\");

// create a buffer of maximum size

int bytesAvailable = fileInputStream.available();

int maxBufferSize = 1000;

// int bufferSize = Math.min(bytesAvailable, maxBufferSize);

byte[] buffer = new byte[bytesAvailable];

// read file and write it into form...

int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

while (bytesRead > 0) {

dos.write(buffer, 0, bytesAvailable);

bytesAvailable = fileInputStream.available();

bytesAvailable = Math.min(bytesAvailable, maxBufferSize);

bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

}

// send multipart form data necessary after file data...

dos.writeBytes(lineEnd);

dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

String serverResponseMessage = conn.getResponseMessage();

BufferedReader rd = new BufferedReader(new InputStreamReader(

conn.getInputStream()));

// String serverResponseCode = conn.

// String serverResponseMessage = conn.getResponseMessage();

while ((result = rd.readLine()) != null) {

Log.v(\"result\", \"result \" + result);

Login.fbResponse = result;

}

// close streams

Log.e(Tag, \"File is written\");

fileInputStream.close();

dos.flush();

dos.close();

rd.close();

} catch (MalformedURLException ex) {

Log.e(Tag, \"error: \" + ex.getMessage(), ex);

}

catch (IOException ioe) {

Log.e(Tag, \"error: \" + ioe.getMessage(), ioe);

}

// Parsing has finished.

return result;

}

public FileUploader(String existingFileName, String urlString) {

this.exsistingFileName = existingFileName;

this.urlString = urlString;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值