Flow.js和Java Servlet上载文件0字节
我按照说明和对java servlet的调用在项目中添加了flow.js:Flow.js和Java Servlet上载文件0字节,java,angularjs,servlets,jakarta-ee,flow-js,Java,Angularjs,Servlets,Jakarta Ee,Flow Js,我按照说明和对java servlet的调用在项目中添加了flow.js: localhost:8080/WebExample/UploadImgServlet?flowChunkNumber=1&flowChunkSize=1048576&flowCurrentChunkSize=693916&flowTotalSize=693916&flowIdentifier=693916-image2png&flowFilename=image2.png&
localhost:8080/WebExample/UploadImgServlet?flowChunkNumber=1&flowChunkSize=1048576&flowCurrentChunkSize=693916&flowTotalSize=693916&flowIdentifier=693916-image2png&flowFilename=image2.png&flowRelativePath=image2.png&flowTotalChunks=1`
在我的servlet中,我获取url的所有参数(flowChuckNumber、flowChuckSize等),但当我尝试获取文件(request.getInputStream())时,它是空的,并且上载0字节
问题在哪里?有什么想法吗
我发现了一个类似的问题,但它是与PHP
我的代码:
HTML(显示图像):
我的Servlet(我遵循以下步骤):
protectedvoidDoService(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{
debug(“[UploadImgServlet-doService]-init”);
int resumableChunkNumber=getResumableChunkNumber(请求);
ResumableInfo=getResumableInfo(请求);
//info包含url的所有流参数。
RandomAccessFile raf=新的RandomAccessFile(info.resumableFilePath,“rw”);
//寻求定位
raf.seek((resumableChunkNumber-1)*信息resumableChunkSize);
//保存到文件
InputStream=request.getInputStream();
长读数=0;
long content_length=request.getContentLength();
//**问题:request.getContentLength返回-1,因此读取0字节**
字节[]字节=新字节[1024*100];
while(读取<内容\长度){
int r=is.read(字节);
if(r<0){
打破
}
写入(字节,0,r);
读数+=r;
}
raf.close();
...
...
您不必担心内容长度。HttpServletRequest
将在正确的点终止输入流。只需一直读到流结束。我想要一个库来上传具有更多选项和视觉吸引力的图像(从文件夹中删除文件、缩略图等)而不是默认的html输入,我无法使用Flowjs和Java Servlet,因此我寻找了另一个库:
有了这个库,我发现使用JavaServlet很容易
如果有人找到使用Flowjs的方法,我不会将此帖子标记为已解决。输入流将为空,因为Flowjs默认使用多部分发布内容。 Java代码的作者指定“Octet”应该用于上传,而不是多部分上传 UploadServlet接受Resumable.js上传,并带有“八位字节” 您需要在init中添加“method:octet”
if try“while((read=uploadedInputStream.read(bytes))!=-1)”结果是一样的,第一次读取是-1,所以上传了0个字节。然后什么都没有发送。我知道,但我想知道为什么文件没有用Flowjs发送,以及如何修复它。我发现:但是结果是一样的,文件没有发送。你试过其他浏览器吗?在检查我的浏览器后,它确实接收数据,但你会发现其他问题即使您收到一些输入,我也会遇到问题,因为Java代码在Javascript方面并不完全是最新的。我认为flow.js在angular方面工作得很好,所以我也尝试使用它,并遇到了与您相同的问题。问题似乎出在Java代码中,它似乎过时了,并且没有考虑currentChunkSize,以及最终认为它没有收到所有的块。我会尝试修复它并让你知道。@user1384975我找到了我说的另一个库,它工作正常,所以我忘记了Flowjs,尽管Flowjs更完整。如果你找到了Flowjs的解决方案,请让我知道。解决方案找到了,我已经发布了。你需要设置八位组的上传类型,或者哈如果没有,请使用NDL多部件post。
...
...
<div flow-init="{singleFile:true}"
flow-file-added="!!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"
flow-files-submitted="$flow.upload()"
flow-file-success="$file.msg = $message">
<div class="drop" flow-drop ng-class="dropClass">
<md-button class="md-raised md-primary" type="file" flow-btn>Upload Image</md-button>
<b>OR</b>
Drag And Drop your image here
</div>
<div class="thumbnail" ng-show="!$flow.files.length">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="Image"/>
</div>
<div class="thumbnail" ng-show="$flow.files.length">
<img flow-img="$flow.files[0]" />
</div>
<table>
<tr ng-repeat="file in $flow.files">
<td>{{$index+1}}</td>
<td>{{file.name}}</td>
<td>{{file.msg}}</td>
</tr>
</table>
</div>
...
...
var app = angular.module("webexample", ['ngMaterial', 'ngNotify','uiGmapgoogle-maps','flow'])
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: '/WebExample/UploadImgServlet',
permanentErrors: [404, 500, 501],
maxChunkRetries: 1,
chunkRetryInterval: 5000,
simultaneousUploads: 1
};
flowFactoryProvider.on('catchAll', function (event) {
console.log('catchAll', arguments);
});
// Can be used with different implementations of Flow.js
// flowFactoryProvider.factory = fustyFlowFactory;
}])
.directive('appDownloadUrl', [function () {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('dragstart', function (event) {
var config = scope.$eval(attrs.appDownloadUrl);
if (!config.disabled) {
var data = config.mime + ':' + config.name + ':' + window.location.href + config.url;
console.log("data: "+data);
event.dataTransfer.setData('DownloadURL', data);
}
});
}
};
}])
.directive("appDragstart", [function () {
return function(scope, element, attrs) {
element.bind('dragstart', function (event) {
scope.$eval(attrs.appDragstart);
});
}
}]).directive("appDragend", [function () {
return function(scope, element, attrs) {
element.bind('dragend', function (event) {
scope.$eval(attrs.appDragend);
});
}
}]).run(function ($rootScope) {
$rootScope.dropEnabled = true;
});
protected void doService(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
LOGGER.debug("[UploadImgServlet - doService] - init");
int resumableChunkNumber = getResumableChunkNumber(request);
ResumableInfo info = getResumableInfo(request);
//info contains all flow parameters of the url.
RandomAccessFile raf = new RandomAccessFile(info.resumableFilePath, "rw");
//Seek to position
raf.seek((resumableChunkNumber - 1) * info.resumableChunkSize);
//Save to file
InputStream is = request.getInputStream();
long readed = 0;
long content_length = request.getContentLength();
//**PROBLEM: request.getContentLength return -1 so read 0 bytes**
byte[] bytes = new byte[1024 * 100];
while(readed < content_length) {
int r = is.read(bytes);
if (r < 0) {
break;
}
raf.write(bytes, 0, r);
readed += r;
}
raf.close();
...
...
Iterator<String> itr = request.getFileNames();
/* Iterate each file, there should only be one/one chunk */
while (itr.hasNext()) {
fileUploaded = request.getFile(itr.next());
raf.write(fileUploaded.getBytes());
}
raf.close();