@Ravi Kumar
您可以使用
public Response uploadFileSimple(@FormDataParam("file") File file, @FormDataParam("file") FormDataContentDisposition formData)
它直接返回一个文件对象,但球衣它会在临时文件中为你创建文件对象,所以你可以说有两个相同大小的临时文件。
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 反射:
@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);
}