DefaultFileItem.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:8k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

Java

  1. package com.gamvan.fileUpload;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.io.UnsupportedEncodingException;
  12. public class DefaultFileItem implements FileItem{
  13.     /**
  14.  * 
  15.  */
  16. private static final long serialVersionUID = 1L;
  17. /**
  18.      * Counter used in unique identifier generation.
  19.      */
  20.     private static int counter = 0;
  21.    /**
  22.      * The name of the form field as provided by the browser.
  23.      */
  24.     private String fieldName;
  25.     /**
  26.      * The content type passed by the browser, or <code>null</code> if
  27.      * not defined.
  28.      */
  29.     private String contentType;
  30.     /**
  31.      * Whether or not this item is a simple form field.
  32.      */
  33.     private boolean isFormField;
  34.     /**
  35.      * The original filename in the user's filesystem.
  36.      */
  37.     private String fileName;
  38.     /**
  39.      * The threshold above which uploads will be stored on disk.
  40.      */
  41.     private int sizeThreshold;
  42.     /**
  43.      * The directory in which uploaded files will be stored, if stored on disk.
  44.      */
  45.     private File repository;
  46.     /**
  47.      * Cached contents of the file.
  48.      */
  49.     private byte[] cachedContent;
  50.     /**
  51.      * Output stream for this item.
  52.      */
  53.     private DeferredFileOutputStream dfos;
  54.     DefaultFileItem(String fieldName, String contentType, boolean isFormField,
  55.                     String fileName, int sizeThreshold, File repository)
  56.     {
  57.         this.fieldName = fieldName;
  58.         this.contentType = contentType;
  59.         this.isFormField = isFormField;
  60.         this.fileName = fileName;
  61.         this.sizeThreshold = sizeThreshold;
  62.         this.repository = repository;
  63.     }
  64.     public InputStream getInputStream()
  65.         throws IOException
  66.     {
  67.         if (!dfos.isInMemory())
  68.         {
  69.             return new FileInputStream(dfos.getFile());
  70.         }
  71.         if (cachedContent == null)
  72.         {
  73.             cachedContent = dfos.getData();
  74.         }
  75.         return new ByteArrayInputStream(cachedContent);
  76.     }
  77.     public String getContentType()
  78.     {
  79.         return contentType;
  80.     }
  81.     public String getName()
  82.     {
  83.         return fileName;
  84.     }
  85.     public boolean isInMemory()
  86.     {
  87.         return (dfos.isInMemory());
  88.     }
  89.     public long getSize()
  90.     {
  91.         if (cachedContent != null)
  92.         {
  93.             return cachedContent.length;
  94.         }
  95.         else if (dfos.isInMemory())
  96.         {
  97.             return dfos.getData().length;
  98.         }
  99.         else
  100.         {
  101.             return dfos.getFile().length();
  102.         }
  103.     }
  104.     public byte[] get()
  105.     {
  106.         if (dfos.isInMemory())
  107.         {
  108.             if (cachedContent == null)
  109.             {
  110.                 cachedContent = dfos.getData();
  111.             }
  112.             return cachedContent;
  113.         }
  114.         byte[] fileData = new byte[(int) getSize()];
  115.         FileInputStream fis = null;
  116.         try
  117.         {
  118.             fis = new FileInputStream(dfos.getFile());
  119.             fis.read(fileData);
  120.         }
  121.         catch (IOException e)
  122.         {
  123.             fileData = null;
  124.         }
  125.         finally
  126.         {
  127.             if (fis != null)
  128.             {
  129.                 try
  130.                 {
  131.                     fis.close();
  132.                 }
  133.                 catch (IOException e)
  134.                 {
  135.                     // ignore
  136.                 }
  137.             }
  138.         }
  139.         return fileData;
  140.     }
  141.     public String getString(String encoding)
  142.         throws UnsupportedEncodingException
  143.     {
  144.         return new String(get(), encoding);
  145.     }
  146.     public String getString()
  147.     {
  148.         return new String(get());
  149.     }
  150.     public void write(File file) throws Exception
  151.     {
  152.         if (isInMemory())
  153.         {
  154.             FileOutputStream fout = null;
  155.             try
  156.             {
  157.                 fout = new FileOutputStream(file);
  158.                 fout.write(get());
  159.             }
  160.             finally
  161.             {
  162.                 if (fout != null)
  163.                 {
  164.                     fout.close();
  165.                 }
  166.             }
  167.         }
  168.         else
  169.         {
  170.             File outputFile = getStoreLocation();
  171.             if (outputFile != null)
  172.             {
  173.                 /*
  174.                  * The uploaded file is being stored on disk
  175.                  * in a temporary location so move it to the
  176.                  * desired file.
  177.                  */
  178.                 if (!outputFile.renameTo(file))
  179.                 {
  180.                     BufferedInputStream in = null;
  181.                     BufferedOutputStream out = null;
  182.                     try
  183.                     {
  184.                         in = new BufferedInputStream(
  185.                             new FileInputStream(outputFile));
  186.                         out = new BufferedOutputStream(
  187.                                 new FileOutputStream(file));
  188.                         byte[] bytes = new byte[2048];
  189.                         int s = 0;
  190.                         while ((s = in.read(bytes)) != -1)
  191.                         {
  192.                             out.write(bytes, 0, s);
  193.                         }
  194.                     }
  195.                     finally
  196.                     {
  197.                         try
  198.                         {
  199.                             in.close();
  200.                         }
  201.                         catch (IOException e)
  202.                         {
  203.                             // ignore
  204.                         }
  205.                         try
  206.                         {
  207.                             out.close();
  208.                         }
  209.                         catch (IOException e)
  210.                         {
  211.                             // ignore
  212.                         }
  213.                     }
  214.                 }
  215.             }
  216.             else
  217.             {
  218.                 /*
  219.                  * For whatever reason we cannot write the
  220.                  * file to disk.
  221.                  */
  222.                 throw new FileUploadException(
  223.                     "Cannot write uploaded file to disk!");
  224.             }
  225.         }
  226.     }
  227.     public void delete()
  228.     {
  229.         cachedContent = null;
  230.         File outputFile = getStoreLocation();
  231.         if (outputFile != null && outputFile.exists())
  232.         {
  233.             outputFile.delete();
  234.         }
  235.     }
  236.     public String getFieldName()
  237.     {
  238.         return fieldName;
  239.     }
  240.     public void setFieldName(String fieldName)
  241.     {
  242.         this.fieldName = fieldName;
  243.     }
  244.     public boolean isFormField()
  245.     {
  246.         return isFormField;
  247.     }
  248.     public void setFormField(boolean state)
  249.     {
  250.         isFormField = state;
  251.     }
  252.     public OutputStream getOutputStream()
  253.         throws IOException
  254.     {
  255.         if (dfos == null)
  256.         {
  257.             File outputFile = getTempFile();
  258.             dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
  259.         }
  260.         return dfos;
  261.     }
  262.     public File getStoreLocation()
  263.     {
  264.         return dfos.getFile();
  265.     }
  266.     protected void finalize()
  267.     {
  268.         File outputFile = dfos.getFile();
  269.         if (outputFile != null && outputFile.exists())
  270.         {
  271.             outputFile.delete();
  272.         }
  273.     }
  274.     protected File getTempFile()
  275.     {
  276.         File tempDir = repository;
  277.         if (tempDir == null)
  278.         {
  279.             tempDir = new File(System.getProperty("java.io.tmpdir"));
  280.         }
  281.         String fileName = "upload_" + getUniqueId() + ".tmp";
  282.         File f = new File(tempDir, fileName);
  283.         f.deleteOnExit();
  284.         return f;
  285.     }
  286.     private static String getUniqueId()
  287.     {
  288.         int current;
  289.         synchronized (DefaultFileItem.class)
  290.         {
  291.             current = counter++;
  292.         }
  293.         String id = Integer.toString(current);
  294.         if (current < 100000000)
  295.         {
  296.             id = ("00000000" + id).substring(id.length());
  297.         }
  298.         return id;
  299.     }
  300. }