我正试图通过我用Java创建的Web服务上传视频。有时也会发送视频,但有时服务器会返回"400错误,参数错误"
你们能帮我吗?
private class SendVideoTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
final String url = server_url + "/rest/cloud/uploadFilePost";
Form form = new Form();
form.add("test_id", test_id);
form.add("report_id", report_id);
String encoded_file = cc.encodeFileToBase64Binary(mediaFile);
form.add("encoded_file", encoded_file);
String resp = "";
CloseableHttpClient httpclient = HttpClients.createSystem();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("test_id", test_id));
urlParameters.add(new BasicNameValuePair("report_id", String
.valueOf(report_id)));
urlParameters.add(new BasicNameValuePair("encoded_file",
encoded_file));
httppost.setEntity(new UrlEncodedFormEntity(urlParameters));
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
resp = EntityUtils.toString(entity);
} finally {
instream.close();
}
}
} finally {
response.close();
httpclient.getConnectionManager().shutdown();
}
return resp;
} catch (Exception e) {
Log.e("VideoAsyncTask (Background)", e.getMessage(), e);
// Toast.makeText(getApplicationContext(),
// "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
return null;
}
}
Web服务java
@Produces(MediaType.TEXT_PLAIN)
@RequestMapping(value = "/rest/cloud/uploadFilePost", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> uploadPostVideo(
@RequestParam(value = "test_id") String test_id,
@RequestParam(value = "report_id") String report_id,
@RequestParam(value = "encoded_file") String encoded_file) {
String output = "";
HttpHeaders responseHeaders = new HttpHeaders();
Logger log = Logger.getLogger("com.vodafone.webmobiletestingsuite");
byte[] bytes = Base64.decodeBase64(encoded_file);
String file_name = "Video" + report_id + ".mp4";
try {
File file = new File(file_name);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
log.info("Starting post cloud");
cloudbo = new CloudBO();
output = cloudbo.uploadFile(report_id, file);
log.info("FILE PATH: " + file.getAbsolutePath());
boolean r = file.delete();
log.info("DELETE: " + r);
log.info("Finish post cloud");
UsabilityReport ur = boUsabilityReports.findById(
Integer.parseInt(report_id), UsabilityReport.class);
log.info("Start attachment");
log.info("REPORT ID:" + ur.getExecutionReportId());
CameraAttachment ca = new CameraAttachment();
ca.setPublicURL(output.split(";")[1]);
log.info("PUBLIC URL:" + output.split(";")[1]);
ca.setUsability_report(ur);
log.info("Finish attachment");
boCameraAttachments.create(ca);
} catch (FileNotFoundException e) {
output = "filenotfound_exception";
} catch (IOException e) {
output = "io_exception";
} catch (DbxException e) {
output = "Dbx: " + e.toString();
}
responseHeaders.set("Access-Control-Allow-Origin", "*");
responseHeaders.set("Access-Control-Allow-Methods",
"POST, GET, PUT, UPDATE, OPTIONS");
responseHeaders.set("Access-Control-Allow-Headers",
"Content-Type, Accept, X-Requested-With");
return new ResponseEntity<String>(output, responseHeaders,
HttpStatus.OK);
}
问题来自服务器端。请尝试在spring中使用MultipartFile类。这里有一个简单的教程可以帮助http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files