DefaultFileItem.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:8k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
Java
- package com.gamvan.fileUpload;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- public class DefaultFileItem implements FileItem{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- /**
- * Counter used in unique identifier generation.
- */
- private static int counter = 0;
- /**
- * The name of the form field as provided by the browser.
- */
- private String fieldName;
- /**
- * The content type passed by the browser, or <code>null</code> if
- * not defined.
- */
- private String contentType;
- /**
- * Whether or not this item is a simple form field.
- */
- private boolean isFormField;
- /**
- * The original filename in the user's filesystem.
- */
- private String fileName;
- /**
- * The threshold above which uploads will be stored on disk.
- */
- private int sizeThreshold;
- /**
- * The directory in which uploaded files will be stored, if stored on disk.
- */
- private File repository;
- /**
- * Cached contents of the file.
- */
- private byte[] cachedContent;
- /**
- * Output stream for this item.
- */
- private DeferredFileOutputStream dfos;
- DefaultFileItem(String fieldName, String contentType, boolean isFormField,
- String fileName, int sizeThreshold, File repository)
- {
- this.fieldName = fieldName;
- this.contentType = contentType;
- this.isFormField = isFormField;
- this.fileName = fileName;
- this.sizeThreshold = sizeThreshold;
- this.repository = repository;
- }
- public InputStream getInputStream()
- throws IOException
- {
- if (!dfos.isInMemory())
- {
- return new FileInputStream(dfos.getFile());
- }
- if (cachedContent == null)
- {
- cachedContent = dfos.getData();
- }
- return new ByteArrayInputStream(cachedContent);
- }
- public String getContentType()
- {
- return contentType;
- }
- public String getName()
- {
- return fileName;
- }
- public boolean isInMemory()
- {
- return (dfos.isInMemory());
- }
- public long getSize()
- {
- if (cachedContent != null)
- {
- return cachedContent.length;
- }
- else if (dfos.isInMemory())
- {
- return dfos.getData().length;
- }
- else
- {
- return dfos.getFile().length();
- }
- }
- public byte[] get()
- {
- if (dfos.isInMemory())
- {
- if (cachedContent == null)
- {
- cachedContent = dfos.getData();
- }
- return cachedContent;
- }
- byte[] fileData = new byte[(int) getSize()];
- FileInputStream fis = null;
- try
- {
- fis = new FileInputStream(dfos.getFile());
- fis.read(fileData);
- }
- catch (IOException e)
- {
- fileData = null;
- }
- finally
- {
- if (fis != null)
- {
- try
- {
- fis.close();
- }
- catch (IOException e)
- {
- // ignore
- }
- }
- }
- return fileData;
- }
- public String getString(String encoding)
- throws UnsupportedEncodingException
- {
- return new String(get(), encoding);
- }
- public String getString()
- {
- return new String(get());
- }
- public void write(File file) throws Exception
- {
- if (isInMemory())
- {
- FileOutputStream fout = null;
- try
- {
- fout = new FileOutputStream(file);
- fout.write(get());
- }
- finally
- {
- if (fout != null)
- {
- fout.close();
- }
- }
- }
- else
- {
- File outputFile = getStoreLocation();
- if (outputFile != null)
- {
- /*
- * The uploaded file is being stored on disk
- * in a temporary location so move it to the
- * desired file.
- */
- if (!outputFile.renameTo(file))
- {
- BufferedInputStream in = null;
- BufferedOutputStream out = null;
- try
- {
- in = new BufferedInputStream(
- new FileInputStream(outputFile));
- out = new BufferedOutputStream(
- new FileOutputStream(file));
- byte[] bytes = new byte[2048];
- int s = 0;
- while ((s = in.read(bytes)) != -1)
- {
- out.write(bytes, 0, s);
- }
- }
- finally
- {
- try
- {
- in.close();
- }
- catch (IOException e)
- {
- // ignore
- }
- try
- {
- out.close();
- }
- catch (IOException e)
- {
- // ignore
- }
- }
- }
- }
- else
- {
- /*
- * For whatever reason we cannot write the
- * file to disk.
- */
- throw new FileUploadException(
- "Cannot write uploaded file to disk!");
- }
- }
- }
- public void delete()
- {
- cachedContent = null;
- File outputFile = getStoreLocation();
- if (outputFile != null && outputFile.exists())
- {
- outputFile.delete();
- }
- }
- public String getFieldName()
- {
- return fieldName;
- }
- public void setFieldName(String fieldName)
- {
- this.fieldName = fieldName;
- }
- public boolean isFormField()
- {
- return isFormField;
- }
- public void setFormField(boolean state)
- {
- isFormField = state;
- }
- public OutputStream getOutputStream()
- throws IOException
- {
- if (dfos == null)
- {
- File outputFile = getTempFile();
- dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
- }
- return dfos;
- }
- public File getStoreLocation()
- {
- return dfos.getFile();
- }
- protected void finalize()
- {
- File outputFile = dfos.getFile();
- if (outputFile != null && outputFile.exists())
- {
- outputFile.delete();
- }
- }
- protected File getTempFile()
- {
- File tempDir = repository;
- if (tempDir == null)
- {
- tempDir = new File(System.getProperty("java.io.tmpdir"));
- }
- String fileName = "upload_" + getUniqueId() + ".tmp";
- File f = new File(tempDir, fileName);
- f.deleteOnExit();
- return f;
- }
- private static String getUniqueId()
- {
- int current;
- synchronized (DefaultFileItem.class)
- {
- current = counter++;
- }
- String id = Integer.toString(current);
- if (current < 100000000)
- {
- id = ("00000000" + id).substring(id.length());
- }
- return id;
- }
- }