pit*_*rio 9 javascript java android web-services cordova
我有一个使用Cordova的HTML5应用程序,您可以从设备上传文件(图像和视频).我必须将用户上传的文件发送到Java WebService,然后将其上传到服务器.
我需要帮助,因为我无法达到我想要的效果.我尝试在互联网上找到的几种解决方案但没有成功
WeService返回下一个异常:
[org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null]
Run Code Online (Sandbox Code Playgroud)
这是我现在的代码:
HTML:
<section id="uploadMedia">
<input type="file" name="fileMedia" id="fileMedia" >
</section>
Run Code Online (Sandbox Code Playgroud)
JS:
var file = $("#uploadMedia").find("#fileMedia")[0].files[0];
if (typeof file !== "undefined") {
uploadFile(file);
}
var uploadFile = function(file, callback) {
// Create a new FormData object
var formData = new FormData();
formData.append('file', file);
$.ajax({
url: WEBSERVICE_URL + "uploadFile",
beforeSend: function(xhr) {
if (WEBSERVICE_USER !== "") {
xhr.setRequestHeader("Authorization", "Basic " + btoa(WEBSERVICE_USER + ":" + WEBSERVICE_PASS));
}
},
data: formData,
method: "POST",
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function(data, textStatus, jqXHR) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("ERROR");
},
complete: function(jqXHR, textStatus) {
if (typeof callback === "function") {
callback();
}
}
});
};
Run Code Online (Sandbox Code Playgroud)
JAVA:
@MultipartConfig
@WebServlet(name = "uploadFile", urlPatterns = {"/uploadFile"})
public class UploadFile extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String json = "";
Part file = request.getPart("file");
String filename = "xcvxcv";
InputStream filecontent = file.getInputStream();
json = "File " + filename + " successfully uploaded";
out.print(json);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我非常感谢各种帮助.
pit*_*rio 10
最后我几天前得到了解决方案.所以,我会回答这个想要了解的人的问题.
JS:
var file = $("#file").files[0]; //this is the input where I can choose the file
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/myWebServiceUrl');
xhr.onload = function () {
//TODO show the progress
};
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
//TODO success callback
}
};
xhr.upload.onprogress = function (event) {
//TODO show the progress
};
xhr.send(formData);
Run Code Online (Sandbox Code Playgroud)
JAVA:
Part filePart = request.getPart("file");
String fileName = String.valueOf("fileName");
File file = new File("/the/path/" + fileName);
OutStream outFile = new FileOutputStream(file);
InputStream filecontent = filePart.getInputStream();
int read = 0;
byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
outFile.write(bytes, 0, read);
}
Run Code Online (Sandbox Code Playgroud)
如果您想了解更多信息,请询问.我希望这能帮到您!!
| 归档时间: |
|
| 查看次数: |
8896 次 |
| 最近记录: |