简体   繁体   English

如何使用Android将.pdf .doc .txt文件上传到服务器(mysql数据库)

[英]How to upload .pdf .doc .txt files to server(mysql database) using Android

I'm facing some difficulties while uploading files to server, I need to know full code of file uploading with namevaluepair. 将文件上传到服务器时遇到一些困难,我需要知道使用namevaluepair上传文件的完整代码。 Hear is my android code, I'm getting only my file path, how to upload it to server with reference as "nameValuePairs.add(new BasicNameValuePair("attachment", selectedFilePath));" 听到的是我的android代码,我只有我的文件路径,如何将其上传到服务器,引用为“ nameValuePairs.add(new BasicNameValuePair(“ attachment”,selectedFilePath));“

       String selectedFilePath="downloads/harry.txt"  //(My file Path)



        InputStream is = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("postiontitle", positiontitle));
        nameValuePairs.add(new BasicNameValuePair("description", description));
        nameValuePairs.add(new BasicNameValuePair("jobtype", jobtype));
        nameValuePairs.add(new BasicNameValuePair("visatype", visatype));



  if (selectedFilePath != null)
        {
            nameValuePairs.add(new BasicNameValuePair("attachment", selectedFilePath));


        }


        try {


            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new                                  HttpPost("http://10.0.3.2/dfsdsd/postjob");
            // HttpPost httpPost = new 
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            int code = response.getStatusLine().getStatusCode();
            String rescode = String.valueOf(code);




            //result=is.toString();
            return rescode;
        } catch (MalformedURLException e) {
            return "No Internet";
        } catch (IOException e) {
            return "No Internet";
        }
    }

If you want simple and fast then i will suggest you ION Library 如果您想简单快速地我会建议您ION Library

Post multipart/form-data and read JSON with an upload progress bar With ION : 发布多部分/表单数据并使用ION使用上传进度栏读取JSON:

Ion.with(getContext())
.load("https://koush.clockworkmod.com/test/echo")
.uploadProgressBar(uploadProgressBar)
.setMultipartParameter("goop", "noop")
.setMultipartFile("archive", "application/zip", new File("/sdcard/filename.zip"))
.asJsonObject()
.setCallback(...)

Use Retrofil or Okhttp. 使用Retrofil或Okhttp。

public static final String MULTIPART_FORM_DATA = "multipart/form-data";

 @NonNull
 private RequestBody createPartFromString(String descriptionString) {  
   return RequestBody.create(
        MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
 }

 @NonNull
 private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {  
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);

// create RequestBody instance from file
RequestBody requestFile =
    RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);

// MultipartBody.Part is used to send also the actual file name
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android的服务器中上传多个选定的文件(即.doc、pdf、jpeg、png) - How to upload multiple selected files (i.e..doc,pdf,jpeg,png) in server at android 如何将像视频音频PDF doc文本文件图像这样的文件从android上传到php服务器 - How to upload files like video audio PDF doc text file image from android to php server 如何使用凌空从Android设备将.doc,.dox和.pdf文件上传到服务器 - How to upload .doc,.dox and .pdf file to server from android device using volley 使用php和android将文件(pdf,doc,jpg,png)上传到我的本地服务器 - Upload file(pdf,doc,jpg,png) to my local server using php with android 如何将 pdf 文件转换为 android 中的 doc 文件? - How to convert pdf files to doc file in android? 将doc,pdf,xls等文件从android应用程序上传到php服务器 - Upload doc, pdf,xls etc, from android application to php server 我们可以在SQLite数据库(Android)中存储.doc .pdf和.jpg文件吗? - Can we store .doc .pdf and .jpg files in SQLite Database (Android)? 使用Android HTTP Post将文件上传到MySQL数据库 - Upload Files to MySQL Database using Android HTTP Post 插入非媒体(pdf,txt,doc)android - Insert Non media (pdf, txt ,doc) android 如何使用php和android将多个图像上传到服务器(mysql数据库) - how to upload more than one image to server(mysql database) using php and android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM