我尝试通过Java创建的webservice上传视频。有时也发送视频,但有时服务器返回" 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;
}
}
webservice 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);
}
答案 0 :(得分:0)
问题来自您的服务器端。尝试在春天使用MultipartFile类。这是一个简单的教程,可以帮助http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files