使用REST服务上传文件 [英] File upload using REST service

查看:189
本文介绍了使用REST服务上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下REST服务(来自本教程)使用泽西多部分实现从不同数量的客户端上传文件到我的GlassFish服务器:

  import java.io 。文件; 
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.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path(/ fileupload)
公共类UploadFileService {

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public响应uploadFile(
@FormDataParam(file)InputStream uploadedInputStream,
@FormDataParam(file)FormDataContentDisposition fileDetail){

String uploadedFileLocation =c:// uploadedFiles / + fileDetail.getFileName();

//保存
saveToFile(uploadedInputStream,uploadedFileLocation);

String output =通过Jersey的RESTFul Webservice上传的文件:+ uploadedFileLocation;

返回Response.status(200).entity(输出).build();
}

//将上传的文件保存到新位置
private void saveToFile(InputStream uploadedInputStream,
String uploadedFileLocation){

try {
OutputStream out = null;
int read = 0;
byte [] bytes = new byte [1024];

out = new FileOutputStream(new File(uploadedFileLocation));
while((read = uploadedInputStream.read(bytes))!= -1){
out.write(bytes,0,read);
}
out.flush();
out.close();
} catch(IOException e){
e.printStackTrace();
}
}
}

这段代码对我来说很好但我注意到以下几点:


  1. 首先将文件上传到服务器的某个地方

  2. 方法uploadFile (...)使用InputStream触发
    上传文件,因此我可以使用
    saveToFile(...)

  3. <在所需位置复制它/ ol>

    我的问题是:




    • 文件在哪里(见上文(1)) )最初存储?

    • 如何删除上传的文件以免费资源?



    UPDATE 1



    附加了InputStream转储:



    这里奇怪的是 - 屏幕截图中的.tmp文件大小为0字节!
    在out.close()之后删除.tmp。

    解决方案

    上传的文件可能保存在内存中(意味着当输入流被gc清理时它将被释放,或者它存储在系统默认的临时文件夹中。 (可能是 System.getProperty(java.io.tmpdir)返回的相同文件夹,这意味着每当您从文件系统中清除临时文件时它都会被清除。 / p>

    具体位置取决于为您处理休息服务的框架。在您的情况下,它似乎是运动衫。



    我不知道球衣在哪里保存这些文件。您可以尝试查看提供的输入流,看看它是什么类型以及存储位置。


    I use following REST service (from this tutorial) to do file uploads from various number of clients to my GlassFish server, using jersey multipart implementation:

    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.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import com.sun.jersey.core.header.FormDataContentDisposition;
    import com.sun.jersey.multipart.FormDataParam;
    
    @Path("/fileupload")
    public class UploadFileService {
    
        @POST
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {
    
            String uploadedFileLocation = "c://uploadedFiles/" + fileDetail.getFileName();
    
            // save it
            saveToFile(uploadedInputStream, uploadedFileLocation);
    
            String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
    
            return Response.status(200).entity(output).build();
        }
    
        // save uploaded file to new location
        private void saveToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {
    
            try {
                OutputStream out = null;
                int read = 0;
                byte[] bytes = new byte[1024];
    
                out = new FileOutputStream(new File(uploadedFileLocation));
                while ((read = uploadedInputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    This code works fine for me but I noticed following:

    1. first the file is uploaded somewhere in the server
    2. the method uploadFile(...) is triggered with an InputStream to the uploaded file, so I can make a copy of it in desired location using saveToFile(...)

    My questions are:

    • where is the file (see (1) above) initially stored?
    • How to delete the uploaded file to free resources?

    UPDATE 1

    Attached the InputStream dump:

    The strange thing here - .tmp file from screenshot is 0 bytes in size! The .tmp is deleted after out.close()

    解决方案

    The uploaded file is probably either kept in memory (meaning it will be freed when the inputstream is clean up by the gc) or it is stored in the system default temp-folder. (Probably the same folder returned by System.getProperty("java.io.tmpdir"), which means that it is cleaned up whenever you clean temporary files from your filesystem.

    The exact location is dependent upon the framework which handles the restservices for you. In your case it appears to be jersey.

    I don't know where jersey saves these files. You could try looking at the provided inputstream to see what type it is and where it's stored.

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

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