[英]How to send file in chunks using Ajax post request to Java Eclipse rest service
I am implementing PDF file upload functionality in my web application where I am trying to read file in javascript and dividing it in chunks using file.slice(start, stop), after reading this I am trying to send it in Ajax post request using loop till all chunks are send to service and like shown below 我正在我的Web应用程序中实现PDF文件上传功能,在这里我试图用javascript读取文件,并使用file.slice(start,stop)将其分成多个块,阅读此文件后,我尝试使用循环在Ajax发布请求中发送该文件直到所有块都发送到服务,如下所示
self.ajaxCallToUploadFile = function (docId, finalChunk, fileName, data) {
var formData = new FormData();
formData.append('file', data);
var serviceUrl = "http://localhost:8080/JerseyDemos/rest/upload/pdf/docId/finalChunk/FileName";
return $.ajax({
url: serviceUrl,
type: 'POST',
success: function (rdata) {
alert(rdata.d);
},
error: function (errorData) {
alert(errorData.responseText);
},
data: formData,
cache: false,
contentType: 'multipart/form-data',
//contentType: false,
processData: false,
async: false
});
};
With file chunk as data I am sending other parameters in query string in service URL. 使用文件块作为数据,我在服务URL的查询字符串中发送其他参数。 I am new to Java and Java services, I am using Eclipse to write services.
我是Java和Java服务的新手,我正在使用Eclipse编写服务。 I tried to read this file chunk as Inputstream but it fails.
我试图将此文件块读取为Inputstream,但失败。 I tried to search for specific answer on this question but everywhere I am getting answer to how to send file from java code to java service.
我试图搜索有关此问题的具体答案,但是到处都在寻找如何将文件从Java代码发送到Java服务的答案。 Here I am expecting answer on how to should send it from javascript to Java rest service and how to read it in Java rest service.
在这里,我期望得到有关如何将其从javascript发送到Java rest服务以及如何在Java rest服务中读取它的答案。
Please help me on this as i am struggling to implement this functionality below is the Java rest service method decoration I tried with. 请为我提供帮助,因为我正在努力实现此功能,以下是我尝试过的Java rest服务方法修饰。
@Path("/upload")
public class JerseyService {
@POST
@Path("/pdf/{docId}/{chunkNumber}/{finalChunk}")
@Consumes({ MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile(@PathParam("docSetId") final Integer docId,
@PathParam("chunkNumber") final Integer chunkNumber, @PathParam("finalChunk") final Boolean finalChunk,
@FormDataParam("file") FormDataBodyPart file)
You have some error in your javascript ajax url 您的javascript ajax网址有一些错误
looking at serviceUrl http://localhost:8080/JerseyDemos/rest/upload/pdf/docId/finalChunk/FileName
there is a mismatch in the expected types of your java Rest Service (RS) parameters and your javascript url: 查看serviceUrl
http://localhost:8080/JerseyDemos/rest/upload/pdf/docId/finalChunk/FileName
,您的Java Rest Service(RS)参数和javascript url的预期类型不匹配 :
docId
is a String in your URL, yet in your Java RS, it is marked as Integer
and its called docSetId
. docId
在您的URL中是一个字符串,但是在您的Java RS中,它被标记为Integer
并被称为docSetId
。
your java RS ist expecting some types: docId -> Integer
, chunkNumber-> Integer
, finalChunk -> Boolean
. 你的java RS IST期待某些类型:
docId -> Integer
, chunkNumber-> Integer
, finalChunk -> Boolean
。 Yet you are sending only Strings as url (docId/finalChunk/FileName). 但是,您仅发送字符串作为url(docId / finalChunk / FileName)。
The java RS mapper can not locate your rest service method to order your request. Java RS映射器找不到您的rest服务方法来订购您的请求。 Just fix your URL.
只需修复您的URL即可。 As Example try:
作为示例,请尝试:
serviceUrl = "http://localhost:8080/JerseyDemos/rest/upload/pdf/203/4/myFileName"
EDIT : 编辑 :
if your parameters are ok, please check that your browser really sends your data chunks to the server. 如果参数正确,请检查浏览器是否确实将数据块发送到服务器。 It can send your other (text) parameters (docId, chunkNumber,and finalchunk), but having trouble sending your binary data.
它可以发送您的其他(文本)参数(docId,chunkNumber和finalchunk),但无法发送二进制数据。 You can check that by using firebug (if you are testing with mozilla) see how to .
您可以使用Firebug(如果您正在使用Mozilla进行测试)进行检查,以了解如何操作 。 Monitor each upload you send to see if they actually leave your browser.
监视您发送的每个上载,以查看它们是否确实离开了浏览器。 Inspect your request to see if a data chunk also leaves, and which name it bears.
检查您的请求,看看是否还有数据块,以及它的名字。
if it doesn't leave your browser, or leave but is not well formed, then the problem is on the client side (your javascript slice and appending code is at fault here). 如果它没有离开您的浏览器,或者没有离开但结构不正确,则问题出在客户端(您的javascript切片和附加代码在这里出错)。
if however it leaves your browser, then try to process the chunk using InputStream
like this: 如果它离开了浏览器,则尝试使用
InputStream
这样处理块:
@Path("/upload")
public class JerseyService {
@POST
@Path("/pdf/{docId}/{chunkNumber}/{finalChunk}")
@Consumes({ MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile(@PathParam("docSetId") final Integer docId,
@PathParam("chunkNumber") final Integer chunkNumber,
@PathParam("finalChunk") final Boolean finalChunk,
@FormDataParam("file") InputStream file){
// do something with file like
int nextByte = -1;
while((nextByte = file.read()) != -1){
// process your byte here
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.