JavaFX:由于缺少边界,文件上传到REST服务/ servlet失败 [英] JavaFX: File upload to REST service / servlet fails because of missing boundary

查看:320
本文介绍了JavaFX:由于缺少边界,文件上传到REST服务/ servlet失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用HttpRequest使用JavaFX上传文件。为了这个目的,我写了下面的函数。

pre $ function $ uploadFile(inputFile:File):void {
//检查文件$ ​​b $ b if(inputFile == null or inputFile.exists())or inputFile.isDirectory()){
return;
}
def httpRequest:HttpRequest = HttpRequest {
location:urlConverter.encodeURL({serverUrl});
source:new FileInputStream(inputFile)
method:HttpRequest.POST
headers:[
HttpHeader {
name:HttpHeader.CONTENT_TYPE
value:multipart / form-data
}
]
}
httpRequest.start();





$ b

在服务器端,我试图使用Apache处理传入数据Commons FileUpload API使用Jersey REST服务。用于执行此操作的代码是Apache主页上的FileUpload教程的简单副本。

  @Path(Upload) 
public class UploadService {

public static final String RC_OK =OK;
public static final String RC_ERROR =ERROR;
$ b $ @ b
@Produces(text / plain)
public String handleFileUpload(@Context HttpServletRequest request){
if(!ServletFileUpload.isMultipartContent(request) ){
返回RC_ERROR;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
列表< FileItem> items = null;
尝试{
items = upload.parseRequest(request);
}
catch(FileUploadException e){
e.printStackTrace();
返回RC_ERROR;




code


然而,我得到一个异常在 items = upload.parseRequest(request);
org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界

我想我必须添加一个手动的边界信息到InputStream。有没有简单的解决方案来做到这一点?或者还有其他的解决方案?

您是否尝试过使用 InputStream HttpServletRequest 类似于这样的

  InputStream is = httpRequest.getInputStream ); 
BufferedInputStream in = new BufferedInputStream(is);
//写出字节

out.close();
is.close();


I'm trying to upload a file using JavaFX using the HttpRequest. For this purpose I have written the following function.

function uploadFile(inputFile : File) : Void {
    // check file
    if (inputFile == null or not(inputFile.exists()) or inputFile.isDirectory()) {
        return;
    }    
    def httpRequest : HttpRequest = HttpRequest {
        location: urlConverter.encodeURL("{serverUrl}");
        source: new FileInputStream(inputFile)
        method: HttpRequest.POST
        headers: [
            HttpHeader {
                name: HttpHeader.CONTENT_TYPE
                value: "multipart/form-data"
            }
        ]
    }
    httpRequest.start();
}

On the server side, I am trying to handle the incoming data using the Apache Commons FileUpload API using a Jersey REST service. The code used to do this is a simple copy of the FileUpload tutorial on the Apache homepage.

@Path("Upload")
public class UploadService {

  public static final String RC_OK = "OK";
  public static final String RC_ERROR = "ERROR";

  @POST
  @Produces("text/plain")
  public String handleFileUpload(@Context HttpServletRequest request) {
    if (!ServletFileUpload.isMultipartContent(request)) {
      return RC_ERROR;
    }
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = null;
    try {
      items = upload.parseRequest(request);
    } 
    catch (FileUploadException e) {
      e.printStackTrace();
      return RC_ERROR;
    }
    ...
  }
}   

However, I get a exception at items = upload.parseRequest(request);: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

I guess I have to add a manual boundary info to the InputStream. Is there any easy solution to do this? Or are there even other solutions?

解决方案

Have you tried just using the InputStream from HttpServletRequest like so

InputStream is = httpRequest.getInputStream();
BufferedInputStream in = new BufferedInputStream(is);
//Write out bytes

out.close();
is.close();

这篇关于JavaFX:由于缺少边界,文件上传到REST服务/ servlet失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆