Java android中上传图像的服务器端(Servlet)代码

Java android中上传图像的服务器端(Servlet)代码,java,android,Java,Android,如何在我的服务器中使用java将从android设备上载的图像保存到特定位置,如C:\Program Files\Tomcat 7.0\Images。 这是我的客户端代码 public static void uploadPictureToServer(String i_file) throws ClientProtocolException,IOException { Bitmap bitmapOrg = BitmapFactory.decodeFile(i_file

如何在我的服务器中使用java将从android设备上载的图像保存到特定位置,如C:\Program Files\Tomcat 7.0\Images。 这是我的客户端代码

public static void uploadPictureToServer(String i_file) throws ClientProtocolException,IOException {
             Bitmap bitmapOrg = BitmapFactory.decodeFile(i_file);

             ByteArrayOutputStream bao = new ByteArrayOutputStream();

             bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

             byte [] ba = bao.toByteArray();

             String ba1=Base64.encode(ba);

             ArrayList<NameValuePair> nameValuePairs = new

             ArrayList<NameValuePair>();

             nameValuePairs.add(new BasicNameValuePair("image",ba1));

             try{

             HttpClient httpclient = new DefaultHttpClient();

             HttpPost httppost = new

             HttpPost(Constant.uploadImage);

             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

             HttpResponse response = httpclient.execute(httppost);

             HttpEntity entity = response.getEntity();

             is = entity.getContent();

             }catch(Exception e){

             Log.e("log_tag", "Error in http connection "+e.toString());

             }
public static void uploadPictureToServer(字符串i_文件)抛出ClientProtocolException、IOException{
位图bitmapOrg=BitmapFactory.decodeFile(i_文件);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.JPEG,90,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encode(ba);
ArrayList nameValuePairs=新建
ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新
HttpPost(恒定上传图像);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
我的工作代码:

doFileUpload功能:

private void doFileUpload(){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null; 
    String exsistingFileName = "/sdcard/six.3gp";
    // Is this the place are you doing something wrong.
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String urlString = "http://192.168.1.5/upload.php";
    try
    {
        Log.e("MediaPlayer","Inside second Method");
        FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        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);
        dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);
        Log.e("MediaPlayer","Headers are written");
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0)
        {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            tv.append(inputLine);
        // close streams
        Log.e("MediaPlayer","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
        Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
    }

    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream ( conn.getInputStream() );
        String str;            
        while (( str = inStream.readLine()) != null)
        {
            Log.e("MediaPlayer","Server Response"+str);
        }
        /*while((str = inStream.readLine()) !=null ){

        }*/
        inStream.close();
    }
    catch (IOException ioex){
        Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
    }
}
upload.php

<?php

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "./upload/".$_FILES["uploadedfile"]["name"]);

mysql_connect("localhost","root","");
        mysql_select_db("chat");


if(isset($_REQUEST['msg']))
{
    $a = $_REQUEST['msg'];
    $sql = "INSERT INTO  upload(image)VALUES('$a')";
    mysql_query($sql);
    }
?>

在java文件中使用doFileUpload函数。请给我一个在java中找不到doFileUpload函数的特定代码