SpringMVC⽂件上传MultipartFile接⼝、FileItem接⼝、
DiskF。。。
⼀.具体类和⽅法
上传⽂件主要⽅法是transferTo(),可以将某个⽂件复制保存到新的路径中。
Multipart接⼝的实现类CommonsMultipartFile类,有个保存⽂件到新路径的⽅法transferTo(),调⽤了FileItem接⼝的⽅法write(),FileItem接⼝的实现类DiskFileItem类
⼆.MultipartFile接⼝源码如下:
public interface MultipartFile {
String getName();
//获取⽂件原名(不包含路径)
String getOriginalFilename();
//获取上传⽂件的类型
String getContentType();
boolean isEmpty();
long getSize();
byte[] getBytes() throws IOException;
InputStream getInputStream() throws IOException;
//保存到⼀个⽬标⽂件中
void transferTo(File var1) throws IOException, IllegalStateException;
}
2.MultipartFile接⼝的实现类CommonsMultipartFile源码如下:
public class CommonsMultipartFile implements MultipartFile, Serializable {
protected static final Log logger = Log(CommonsMultipartFile.class);
private final FileItem fileItem;
private final long size;
public CommonsMultipartFile(FileItem fileItem) {
this.fileItem = fileItem;
this.size = Size();
}
public final FileItem getFileItem() {
return this.fileItem;
}
public String getName() {
return FieldName();
}
public String getOriginalFilename() {
String filename = Name();
if(filename == null) {
return "";
} else {
int pos = filename.lastIndexOf("/");
if(pos == -1) {
pos = filename.lastIndexOf("\\");
}
return pos != -1?filename.substring(pos + 1):filename;
}
}
public String getContentType() {
return ContentType();
}
public boolean isEmpty() {
return this.size == 0L;
}
public long getSize() {
return this.size;
}
public byte[] getBytes() {
if(!this.isAvailable()) {
throw new IllegalStateException("File has been moved - cannot be read again");
} else {
byte[] bytes = ();
return bytes != null?bytes:new byte[0];
}
}
public InputStream getInputStream() throws IOException {
if(!this.isAvailable()) {
throw new IllegalStateException("File has been moved - cannot be read again");
} else {
InputStream inputStream = InputStream();
return (InputStream)(inputStream != null?inputStream:new ByteArrayInputStream(new byte[0]));
}
}
public void transferTo(File dest) throws IOException, IllegalStateException {
if(!this.isAvailable()) {
throw new IllegalStateException("File has already been moved - cannot be transferred again");
} else ists() && !dest.delete()) {
throw new IOException("Destination file [" + AbsolutePath() + "] already exists and could not be deleted");
} else {
try {
this.fileItem.write(dest);
if(logger.isDebugEnabled()) {
String ex = "transferred";
if(!this.fileItem.isInMemory()) {
ex = this.isAvailable()?"copied":"moved";
}
logger.debug("Multipart file \'" + Name() + "\' with original filename [" + OriginalFilename() + "], stored " +
}
} catch (FileUploadException var3) {
throw new Message());
} catch (IOException var4) {
throw var4;
} catch (Exception var5) {
<("Could not transfer to file", var5);
throw new IOException("Could not transfer to file: " + Message());
}
}
}
protected boolean isAvailable() {
return this.fileItem.isInMemory()?true:(this.fileItem instanceof DiskFileItem?((DiskFileItem)this.fileItem).getStoreLocation().exists():Size() == this.size);
}
public String getStorageDescription() {
return this.fileItem.isInMemory()?"in memory":(this.fileItem instanceof DiskFileItem?"at [" +
((DiskFileItem)this.fileItem).getStoreLocation().getAbsolutePath() + "]":"on disk");
}
}
三.FileItem接⼝,具体的api介绍如以下链接:
1.  boolean isFormField()
isFormField⽅法⽤于判断FileItem类对象封装的数据是⼀个普通⽂本表单字段,还是⼀个⽂件表单字段,如果是普通表单字段则返回true,否则返回false。因此,可以使⽤该⽅法判断是否为普通表单域,还是⽂件上传表单域。
2.  String getName()
getName⽅法⽤于获得⽂件上传字段中的⽂件名。
注意IE或FireFox中获取的⽂件名是不⼀样的,IE中是绝对路径,FireFox中只是⽂件名。
3.  String getFieldName()
getFieldName⽅法⽤于返回表单标签name属性的值。如上例中<input type="text" name="column" />的value。
4.  void write(File file)
write⽅法⽤于将FileItem对象中保存的主体内容保存到某个指定的⽂件中。如果FileItem对象中的主体内容是保存在某个临时⽂件中,该⽅法顺利完成后,临时⽂件有可能会被清除。该⽅法也可将普通表单字段内容写⼊到⼀个⽂件中,但它主要⽤途是将上传的⽂件内容保存在本地⽂件系统中。
5.  String getString()
getString⽅法⽤于将FileItem对象中保存的数据流内容以⼀个字符串返回,它有两个重载的定义形式:
public java.lang.String getString()
public java.lang.String getString(java.lang.String encoding)
throws java.io.UnsupportedEncodingException
前者使⽤缺省的字符集编码将主体内容转换成字符串,后者使⽤参数指定的字符集编码将主体内容转换成字符串。如果在读取普通表单字段元素的内容时出现了中⽂乱码现象,请调⽤第⼆个getString⽅法,并为之传递正确的字符集编码名称。
6.  String getContentType()
getContentType ⽅法⽤于获得上传⽂件的类型,即表单字段元素描述头属性“Content-Type”的值,如“image/jpeg”。如果FileItem类对象对应的是普通表单字段,该⽅法将返回null。
7.  boolean isInMemory()
isInMemory⽅法⽤来判断FileItem对象封装的数据内容是存储在内存中,还是存储在临时⽂件中,如果存储在内存中则返回true,否则返回false。
8.  void delete()
delete⽅法⽤来清空FileItem类对象中存放的主体内容,如果主体内容被保存在临时⽂件中,delete⽅法将删除该临时⽂件。
尽管当FileItem对象被垃圾收集器收集时会⾃动清除临时⽂件,但及时调⽤delete⽅法可以更早的清除临时⽂件,释放系统存储资源。另外,当系统出现异常时,仍有可能造成有的临时⽂件被永久保存在了硬盘中。
9.  InputStream getInputStream()
以流的形式返回上传⽂件的数据内容。
10. long getSize()
返回该上传⽂件的⼤⼩(以字节为单位)。
四.FileItem的实现类DiskFileItem源码如下:
重点看write()⽅法。
public class DiskFileItem implements FileItem {
private static final long serialVersionUID = 2237570099615271025L;
public static final String DEFAULT_CHARSET = "ISO-8859-1";
private static final String UID = UUID.randomUUID().toString().replace('-', '_');
private static final AtomicInteger COUNTER = new AtomicInteger(0);
private String fieldName;
private final String contentType;
private boolean isFormField;
private final String fileName;
private long size = -1L;
private final int sizeThreshold;
private final File repository;
private byte[] cachedContent;
private transient DeferredFileOutputStream dfos;
springmvc常用标签
private transient File tempFile;
private File dfosFile;
private FileItemHeaders headers;
public DiskFileItem(String fieldName, String contentType, boolean isFormField, String fileName, int sizeThreshold, File repository) {
this.fieldName = fieldName;
this.isFormField = isFormField;
this.fileName = fileName;
this.sizeThreshold = sizeThreshold;
}
public InputStream getInputStream() throws IOException {
if(!this.isInMemory()) {
return new FileInputStream(File());
} else {
if(this.cachedContent == null) {
this.cachedContent = Data();
}
return new ByteArrayInputStream(this.cachedContent);
}
}
public String getContentType() {
tType;
}
public String getCharSet() {
ParameterParser parser = new ParameterParser();
parser.setLowerCaseNames(true);
Map params = parser.ContentType(), ';');
return (("charset");
}
public String getName() {
return Streams.checkFileName(this.fileName);
}
public boolean isInMemory() {
return this.cachedContent != null?true:this.dfos.isInMemory();
}
public long getSize() {
return this.size >= 0L?this.size:(this.cachedContent != null?(long)this.cachedContent.length:(this.dfos.isInMemory()? (long)Data().length:File().length()));
}
public byte[] get() {
if(this.isInMemory()) {
if(this.cachedContent == null) {
this.cachedContent = Data();
}
return this.cachedContent;
} else {
byte[] fileData = new byte[(Size()];
BufferedInputStream fis = null;
try {
fis = new BufferedInputStream(new FileInputStream(File()));
} catch (IOException var12) {
fileData = null;
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException var11) {
;
}
}
}
return fileData;
}
}
}
public String getString(String charset) throws UnsupportedEncodingException { return new (), charset);
}
public String getString() {
byte[] rawdata = ();
String charset = CharSet();
if(charset == null) {
charset = "ISO-8859-1";
}
try {
return new String(rawdata, charset);
} catch (UnsupportedEncodingException var4) {
return new String(rawdata);
}
}
public void write(File file) throws Exception {
if(this.isInMemory()) {
FileOutputStream outputFile = null;
try {
outputFile = new FileOutputStream(file);
outputFile.());
} finally {
if(outputFile != null) {
outputFile.close();
}
}
} else {
File outputFile1 = StoreLocation();
if(outputFile1 == null) {
throw new FileUploadException("Cannot write uploaded file to disk!");            }
this.size = outputFile1.length();
if(!ameTo(file)) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(outputFile1));
out = new BufferedOutputStream(new FileOutputStream(file));
} finally {
if(in != null) {
try {
in.close();
} catch (IOException var19) {
;
}
}
if(out != null) {
try {
out.close();
} catch (IOException var18) {
;
}
}
}
}
}
}
public void delete() {
this.cachedContent = null;
File outputFile = StoreLocation();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。