选择多个文件并使用泽西岛上传 [英] Selecting multiple files and uploading them using Jersey

查看:159
本文介绍了选择多个文件并使用泽西岛上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Jersey进行多个文件上传的帮助.我使用以下代码使用泽西岛上传了一个文件.

I need help with multiple file uploads using Jersey. I used the following code to upload a single file using Jersey.

package my.first.rest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;



import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("uploadfile")
public class Upload {
String location;



    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public  String uploadfile(@FormDataParam("file") InputStream is, @FormDataParam("file") FormDataContentDisposition filedetail){



        saveToDisk(is,filedetail);
        return  "File Uploaded Succesfully_"+location;

    }


    private void saveToDisk(InputStream is1,
            FormDataContentDisposition filedetail) {
        // TODO Auto-generated method stub

         location = "E://upload/"+filedetail.getFileName();
        try{
            OutputStream out = new FileOutputStream(new File(location));
            int read = 0;
            byte[] bytes = new byte[1024];
            out = new FileOutputStream (new File(location));
            while((read = is1.read(bytes)) != -1){
                out.write(bytes,0,read);
            }
            out.flush();

            out.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }


}

上面的代码可以很好地上传单个文件,但是当我在输入类型= file标签中添加multiple ="multiple"属性时,我可以在单个上传中选择多个项目,它可以上传第一个选定的文件项目和最后选择的项目的名称.我不希望代码能正常工作,因为它本来不是要处理多个文件上传的,但是必须有一种解决方法,对吗?从那以后,它将使用一个文件,并使用另一个文件的名称.

The above code works well with uploading a single file but when I add the multiple="multiple" attribute to the input type = file tag, I'm able to select multiple items in a single upload, it uploads the first selected item and the name of the last selected item. I'm not expecting the code to work cause it wasn't meant to handle multiple file uploads but there has to be a way around it, right?? Since, it's taking one file and the name of another one.

我看过多个stackoverflow线程,并且用谷歌搜索了很多.如果找到答案,我将不会发布此信息.我不希望使用以下代码类型上传多个文件:

I have looked multiple stackoverflow threads and googled a lot. I wouldn't have posted this if I had found the answer. I'm not looking for uploading multiple files using the types of code below:

<input type ="file" name="file">
<input type ="file" name="file">
<input type ="file" name="file2">

我不想分别上传多个文件.我希望能够一次选择多个文件,然后将所有文件上传到某个地方.我的标签是这样的:

I don't want to upload multiple files individually. I want to be able to select multiple files at a time and all of them to be uploaded somewhere. My tag is like this:

<input type ="file" name="file" multiple="multiple">

这是完整的HTML代码.

Here's the entire HTML code.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action ="http://localhost:8080/fuseframework/uploadfile/upload" method="post" enctype="multipart/form-data">
file: 
<br>
<input type ="file" name="file" multiple="multiple">

<input type="submit" value="send">
</form>
</body>
</html>

这些是我用过的罐子 http://i.stack.imgur.com/1tVT8.png

These are the jars I've used http://i.stack.imgur.com/1tVT8.png

推荐答案

使用'FormDataMultiPart'可以正常工作.

It works OK for me to use the 'FormDataMultiPart'.

这是Java代码,FormDataContentDisposition对象(formParams)包含实际的文件内容.

Here is the Java code, the FormDataContentDisposition object(formParams) contains actual file content.

List<FormDataBodyPart> parts = formParams.getFields("file");
for (FormDataBodyPart part : parts) {
    FormDataContentDisposition file = part.getFormDataContentDisposition();
}

在JS端,我使用FormData对象并推送具有相同名称的多个文件:

In the JS side, I use the FormData object and push several files with the same name:

for (var i = 0; i < files.length; i++)
    fd.append('file', files[i]);

希望会有所帮助

这篇关于选择多个文件并使用泽西岛上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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