文件在服务器中存储,如何发送音频文件在服务器中存储

我写这段代码发送一个文件到服务器:

路径=到文件要发送的绝对路径。其余的参数似乎不言自明。

public static String sendFileToServer(String path, String urlString,

String mimeType, String id) throws Exception {

try {

String URL = "", file_name = "";

if (!path.equals("")) {

file_name = path.substring(path.lastIndexOf("/") + 1);

}

File directory = new File(path);

byte[] data = new byte[(int) directory.length()];

try {

FileInputStream fileInputStream = new FileInputStream(directory);

fileInputStream.read(data);

} catch (FileNotFoundException e) {

System.out.println("File Not Found.");

e.printStackTrace();

} catch (IOException e1) {

System.out.println("Error Reading The File.");

e1.printStackTrace();

}

URL = Constants.WEB_ROOT_URL + urlString;

Log.v("AppEngine Manager", "Image Upload URL: " + URL);

HttpClient httpClient = new DefaultHttpClient();

HttpPost postRequest = new HttpPost(URL);

ByteArrayBody bab = new ByteArrayBody(data, file_name);

MultipartEntity reqEntity = new MultipartEntity(

HttpMultipartMode.BROWSER_COMPATIBLE);

reqEntity.addPart("media_type", new StringBody(mimeType));

reqEntity.addPart("file_name", new StringBody(file_name));

reqEntity.addPart("file_id", new StringBody(file_name));

reqEntity.addPart("file", bab);

postRequest.setEntity(reqEntity);

HttpResponse response = httpClient.execute(postRequest);

BufferedReader reader = new BufferedReader(new InputStreamReader(

response.getEntity().getContent(), "UTF-8"));

String sResponse;

StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {

s = s.append(sResponse);

}

return s + "";

} catch (Exception e) {

// handle exception here

Log.e(e.getClass().getName(), e.getMessage());

return "exception";

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值