com.rt.web.multipart.GMultipartFile Maven / Gradle / Ivy
package com.rt.web.multipart;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* This src is modified from {@link org.springframework.web.multipart.commons.CommonsMultipartFile CommonsMultipartFile}
* and renamed to GMultipartResolver to make it work in GAE.
*
*
MultipartFile implementation for Jakarta Commons FileUpload.
*
*
* NOTE: As of Spring 2.0, this class requires Commons FileUpload 1.1 or higher. The
* implementation does not use any deprecated FileUpload 1.0 API anymore, to be compatible with
* future Commons FileUpload releases.
*
* @author kernel164
* @author Trevor D. Cook
* @author Juergen Hoeller
* @see GMultipartResolver
*/
public class GMultipartFile implements MultipartFile, Serializable {
private static final long serialVersionUID = -2270490133870606L;
protected static final Log logger = LogFactory.getLog(GMultipartFile.class);
private final FileItem fileItem;
private final long size;
/**
* Create an instance wrapping the given FileItem.
*
* @param fileItem the FileItem to wrap
*/
public GMultipartFile(FileItem fileItem) {
this.fileItem = fileItem;
this.size = this.fileItem.getSize();
}
/**
* Return the underlying org.apache.commons.fileupload.FileItem
instance. There is
* hardly any need to access this.
*
* @return FileItem
*/
public final FileItem getFileItem() {
return this.fileItem;
}
/**
* Get file name.
*
* @return String
*/
public String getName() {
return this.fileItem.getFieldName();
}
/**
* Get original file name.
*
* @return String
*/
public String getOriginalFilename() {
String filename = this.fileItem.getName();
if (filename == null) {
// Should never happen.
return "";
}
// check for Unix-style path
int pos = filename.lastIndexOf("/");
if (pos == -1) {
// check for Windows-style path
pos = filename.lastIndexOf("\\");
}
if (pos != -1) {
// any sort of path separator found
return filename.substring(pos + 1);
} else {
// plain name
return filename;
}
}
/**
* Get file content type.
*
* @return String
*/
public String getContentType() {
return this.fileItem.getContentType();
}
/**
* Is empty file?
*
* @return boolean
*/
public boolean isEmpty() {
return (this.size == 0);
}
/**
* Get file size.
*
* @return long
*/
public long getSize() {
return this.size;
}
/**
* Get file content as byte array.
*
* @return byte[]
*/
public byte[] getBytes() {
byte[] bytes = this.fileItem.get();
return (bytes != null ? bytes : new byte[0]);
}
/**
* Get input stream.
*
* @return InputStream
*/
public InputStream getInputStream() throws IOException {
InputStream inputStream = this.fileItem.getInputStream();
return (inputStream != null ? inputStream : new ByteArrayInputStream(new byte[0]));
}
/**
* This method is not supported in GAE.
*
* @param dest dest
* @throws IOException IOException
* @throws IllegalStateException IllegalStateException
*/
public void transferTo(File dest) throws IOException, IllegalStateException {
throw new UnsupportedOperationException("not possible.");
}
/**
* Determine whether the multipart content is still available. Always true.
*
* @return boolean
*/
protected boolean isAvailable() {
return true;
}
/**
* Return a description for the storage location of the multipart content.
* In this implementation, it returns "in memory" always.
*
* @return string
*/
public String getStorageDescription() {
return "in memory";
}
}