将图像上传到服务器

迪尔希
public class Upload {

ProgressDialog dialog = null;
int serverResponseCode = 0;
String uploadFilePath = null;
String uploadFileName = null;
String msg = null;
String upLoadServerUri = " http://192.168.1.179/index.php";
protected MainActivity context;

public Upload(MainActivity context) {
    this.context = context;
}

public int uploadFile(String sourceFileUri) {


    String fileName = sourceFileUri;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {

        context.dialog.dismiss();



        return 0;

    } else {
        try {

            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadServerUri);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", fileName);

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

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                    + fileName + "\"" + lineEnd);

            dos.writeBytes(lineEnd);

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();

            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // read file and write it into form...
            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);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);

            if (serverResponseCode == 200) {


                InputStream is = conn.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer response = new StringBuffer();
                while ((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
                context.q_no.setText(response);

                fileInputStream.close();
                dos.flush();
                dos.close();











                context.runOnUiThread(new Runnable() {
                    public void run() {

                        String msg = "yes";

                        //context.messageText.setText(msg);
                        Toast.makeText(context, "File Upload Complete.",
                                Toast.LENGTH_SHORT).show();
                    }
                });

            }
     } catch (MalformedURLException ex) {

     Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            context.dialog.dismiss();
            e.printStackTrace();


            Log.e("Upload file to server Exception", "Exception : "
                    + e.getMessage(), e);
        }
        context.dialog.dismiss();
        return serverResponseCode;

     }
  }
}

我尝试了上面的代码来捕获图像并将其上传到php server。当我上传图像服务器返回一个值时,它工作正常。但是当我尝试此代码时,在图像上传到服务器和服务器响应后出现以下异常。

  07-27 10:50:55.303: W/System.err(4649):     android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
323go

您正确地upload在单独的线程上运行(AsyncTask?)-很好。但是,无论何时与UI交互(.dialog.dismiss().setText(...)等),都需要在UI线程上运行它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过javascript将图像上传到服务器

来自分类Dev

将图像上传到服务器时重命名

来自分类Dev

正在将图像上传到服务器,但未显示

来自分类Dev

将图像上传到服务器不起作用

来自分类Dev

将图像从android上传到PHP服务器

来自分类Dev

无法将图像上传到服务器ios

来自分类Dev

从iOS将图像上传到PHP服务器

来自分类Dev

将压缩图像上传到PHP服务器

来自分类Dev

ExpressJS Multer:将图像上传到服务器

来自分类Dev

Flutter Dio:无法将图像上传到服务器

来自分类Dev

iOS通过ASP将图像上传到服务器

来自分类Dev

从PictureBox将图像上传到服务器

来自分类Dev

无法将图像上传到服务器ios

来自分类Dev

将压缩的图像上传到PHP服务器

来自分类Dev

将图像从android上传到PHP服务器

来自分类Dev

PHP将图像上传到服务器

来自分类Dev

从Android设备将图像上传到服务器

来自分类Dev

正在将图像上传到服务器,但未显示

来自分类Dev

将图像上传到服务器symfony2

来自分类Dev

将转换后的图像上传到服务器

来自分类Dev

将图像上传到服务器不起作用

来自分类Dev

使用AsyncTask将图像或视频上传到服务器

来自分类Dev

将调整大小的表单图像上传到服务器

来自分类Dev

无法将经过改造的图像上传到服务器

来自分类Dev

Java Android将图像上传到服务器

来自分类Dev

Android无法将图像上传到服务器

来自分类Dev

Xamarin:将图像上传到服务器

来自分类Dev

使用tinymce将图像上传到服务器

来自分类Dev

使用改造将图像上传到服务器