把mp3文件放到服务器,Android:如何将.mp3文件和图像上传到http服务器?

我将图片上传到服务器的代码是:

String userIdParameter = String.valueOf(userId);

String fileName = "temporary_holder.jpg";

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;

String sourceFileUri = HomeScreen.get_path();

String upLoadServerUri = "http://10.120.10.87:8080/WebImage/UploadImage";

File sourceFile = new File(sourceFileUri);

if (!sourceFile.isFile()) {

Log.e("Huzza", "Source File Does not exist");

return;

}

int serverResponseCode = 0;

try {

// open a URL connection to the Servlet

FileInputStream fileInputStream = new FileInputStream(sourceFile);

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

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

// Use a post method.

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("file_name", fileName);

conn.setRequestProperty("file_name_audio", fileName);

conn.setRequestProperty("X-myapp-param1", userIdParameter);

// conn.setFixedLengthStreamingMode(1024);

// conn.setChunkedStreamingMode(1);

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

dos.writeBytes(twoHyphens + boundary + lineEnd);

dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\""

+ fileName + "\"" + lineEnd);

dos.writeBytes(lineEnd);

// create a buffer of maximum size

bytesAvailable = fileInputStream.available();

int streamSize = (int) sourceFile.length();

bufferSize = streamSize / 10;

System.out.println("streamSize" + streamSize);

buffer = new byte[streamSize];

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

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

int count = 0;

while (bytesRead > 0) {

progress = (int) (count);

displayNotification();

Thread.sleep(500);

dos.write(buffer, 0, bufferSize);

bytesAvailable = fileInputStream.available();

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

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

count += 10;

}

// 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();

System.out.println("Upload file to serverHTTP Response is : "

+ serverResponseMessage + ": " + serverResponseCode);

// close streams

System.out.println("Upload file to server" + fileName

+ " File is written");

fileInputStream.close();

dos.flush();

dos.close();

} catch (MalformedURLException ex) {

ex.printStackTrace();

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

} catch (Exception e) {

e.printStackTrace();

}

// this block will give the response of upload link

try {

BufferedReader rd = new BufferedReader(new InputStreamReader(

conn.getInputStream()));

String line;

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

System.out.println("RESULT Message: " + line);

}

rd.close();

} catch (IOException ioex) {

Log.e("Huzza", "error: " + ioex.getMessage(), ioex);

}

return; // like 200 (Ok)

将图像上传到服务器工作正常。.我需要同时将mp3文件和图像上传到服务器。.请帮助

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值