java 使用 REST 服务上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17610221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File upload using REST service
提问by filip_j
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:
我使用以下 REST 服务(来自本教程)使用 jersey 多部分实现将文件从不同数量的客户端上传到我的 GlassFish 服务器:
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:
这段代码对我来说很好用,但我注意到以下几点:
- first the file is uploaded somewhere in the server
- 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(...)
- 首先文件被上传到服务器的某个地方
- 方法 uploadFile(...) 使用 InputStream 触发到上传的文件,因此我可以使用 saveToFile(...) 在所需位置制作它的副本
My questions are:
我的问题是:
- where is the file (see (1) above) initially stored?
- How to delete the uploaded file to free resources?
- 文件(见上面的(1))最初存储在哪里?
- 如何删除上传的文件以释放资源?
UPDATE 1
更新 1
Attached the InputStream dump:
附上 InputStream 转储:
The strange thing here - .tmp file from screenshot is 0 bytes in size! The .tmp is deleted after out.close()
这里很奇怪 - 屏幕截图中的 .tmp 文件大小为 0 字节!.tmp 在 out.close() 后被删除
采纳答案by Buurman
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.
上传的文件可能保存在内存中(意味着当输入流被 gc 清理时它将被释放)或者它存储在系统默认的临时文件夹中。(可能与 返回的文件夹相同System.getProperty("java.io.tmpdir")
,这意味着每当您从文件系统中清除临时文件时,它就会被清除。
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.
我不知道 jersey 在哪里保存这些文件。您可以尝试查看提供的输入流以查看它是什么类型以及它的存储位置。