[英]How to use temp file from which i get inputStream
我正在嘗試使用Rest Web Service將文件上傳到Web。 我創建了一個流程,該流程接受以下形式的文件:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetails,
@FormDataParam("location") String uploadedFileLoc ) {
.
.
}
在此方法的末尾,我需要返回客戶端上傳的文件,在下一個方法中,該文件將轉換為字節數組。 基本上我以“ InputStream”格式獲取文件。 我可以通過某種方式自己獲取文件嗎?
@Ravi Kumar
您可以使用
public Response uploadFileSimple(@FormDataParam("file") File file, @FormDataParam("file") FormDataContentDisposition formData)
它直接返回一個File對象,但是它會為您自己在temp中自動創建File對象,因此您可以說有兩個大小相同的temp文件。
First MIMExxxxx.tmp from where you get you input steam
Second re234xxxx from which you get this file object
NOTE :
在調用資源方法jersey之前,需要花一些時間來創建這兩個文件(根據文件大小)。
而且,如果您想直接使用MIMExx.tmp
文件,那么我不認為有直接的方法可以使用JAVA REFLECTION
獲取臨時文件的路徑並使用。
使用Java Reflection:
@Path("/upload/{parentfolderid}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFileWithPart(FormDataMultiPart form)
{
try
{
FormDataBodyPart filePart = form.getField("upload");
BodyPartEntity bodyPart = (BodyPartEntity) filePart.getEntity();
MIMEPart mimePart = (MIMEPart) readFieldValue("mimePart", bodyPart);
Object dataHead = readFieldValue("dataHead", mimePart);
Object dataFile = readFieldValue("dataFile", dataHead);
File tempFile = null;
if (dataFile != null)
{
Object weakDataFile = readFieldValue("weak", dataFile);
tempFile = (File) readFieldValue("file", weakDataFile);
}
else
{
tempFile = filePart.getValueAs(File.class);
}
// Here is your *tempFile*, Do what ever you want with it
}
private static Object readFieldValue(String fieldName, Object o) throws Exception
{
Field field = o.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(o);
}
文件是文件,流是流。
如果您需要流中的文件,則需要創建文件並將流寫入文件中。
您可以為此使用temp文件夾: 如何在Java中創建一個臨時文件而不將隨機數附加到文件名中?
並在那里創建一個臨時文件。
UPD
對於您的需求,File.createTempFile()應該足夠了。
您可以使用Jersey REST API上傳文件。 例如,
@Path("/upload")
public class FileUploadService{
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String location = "/" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, location);
String output = "File uploaded to : " + location;
return Response.status(200).entity(output).build();
}
}
並使用以下方法保存文件:
private void writeToFile(InputStream uploadedInputStream,
String location) {
try {
OutputStream out = new FileOutputStream(new File(
location));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(location));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
希望能幫助到你!
謝謝大家的建議。 我需要做的主要事情是將文件轉換為字節數組。 由於我正在獲取InputStream,因此可以將其作為返回到下一個流的傳遞,而不是等待文件本身。 在下一個流程中,而不是使用“文件到字節數組”轉換器,只需使用“對象到字節數組”轉換器。 終於可以了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.