File.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:7k
- package com.jspsmart.upload;
- import java.io.ByteArrayInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.servlet.ServletException;
- public class File
- {
- private SmartUpload m_parent;
- private int m_startData;
- private int m_endData;
- private int m_size;
- private String m_fieldname;
- private String m_filename;
- private String m_fileExt;
- private String m_filePathName;
- private String m_contentType;
- private String m_contentDisp;
- private String m_typeMime;
- private String m_subTypeMime;
- private String m_contentString;
- private boolean m_isMissing;
- public static final int SAVEAS_AUTO = 0;
- public static final int SAVEAS_VIRTUAL = 1;
- public static final int SAVEAS_PHYSICAL = 2;
- File()
- {
- m_startData = 0;
- m_endData = 0;
- m_size = 0;
- m_fieldname = new String();
- m_filename = new String();
- m_fileExt = new String();
- m_filePathName = new String();
- m_contentType = new String();
- m_contentDisp = new String();
- m_typeMime = new String();
- m_subTypeMime = new String();
- m_contentString = new String();
- m_isMissing = true;
- }
- public void fileToField(ResultSet rs, String columnName)
- throws SQLException, SmartUploadException, IOException, ServletException
- {
- long numBlocks = 0L;
- int blockSize = 0x10000;
- int leftOver = 0;
- int pos = 0;
- if (rs == null)
- throw new IllegalArgumentException("The RecordSet cannot be null (1145).");
- if (columnName == null)
- throw new IllegalArgumentException(
- "The columnName cannot be null (1150).");
- if (columnName.length() == 0)
- throw new IllegalArgumentException(
- "The columnName cannot be empty (1155).");
- numBlocks =
- BigInteger.valueOf(m_size).divide(BigInteger.valueOf(blockSize)).
- longValue();
- leftOver =
- BigInteger.valueOf(m_size).mod(BigInteger.valueOf(blockSize)).intValue();
- try
- {
- for (int i = 1; (long) i < numBlocks; i++)
- {
- rs.updateBinaryStream(
- columnName,
- new ByteArrayInputStream(m_parent.m_binArray, pos, blockSize),
- blockSize);
- pos = pos != 0 ? pos : 1;
- pos = i * blockSize;
- }
- if (leftOver > 0)
- rs.updateBinaryStream(
- columnName,
- new ByteArrayInputStream(m_parent.m_binArray, pos, leftOver),
- leftOver);
- }
- catch (SQLException e)
- {
- byte binByte2[] = new byte[m_size];
- System.arraycopy(m_parent.m_binArray, m_startData, binByte2, 0, m_size);
- rs.updateBytes(columnName, binByte2);
- }
- catch (Exception e)
- {
- throw new SmartUploadException(
- "Unable to save file in the DataBase (1130).");
- }
- }
- public byte getBinaryData(int index)
- {
- if (m_startData + index > m_endData)
- throw new ArrayIndexOutOfBoundsException("Index Out of range (1115).");
- if (m_startData + index <= m_endData)
- return m_parent.m_binArray[m_startData + index];
- else
- return 0;
- }
- public String getContentDisp()
- {
- return m_contentDisp;
- }
- public String getContentString()
- {
- String strTMP = new String(m_parent.m_binArray, m_startData, m_size);
- return strTMP;
- }
- public String getContentType()
- {
- return m_contentType;
- }
- protected int getEndData()
- {
- return m_endData;
- }
- public String getFieldName()
- {
- return m_fieldname;
- }
- /* 获取文件数据信息 */
- public byte[] getFileBinaryData()
- {
- try
- {
- byte[] file_data = new byte[m_size];
- for (int i = 0; i < m_size; i++)
- //fileOut.write(m_parent.m_binArray, m_startData, m_size);
- file_data[i] = m_parent.m_binArray[m_startData + i];
- return file_data;
- }
- catch (Exception e)
- {
- return null;
- }
- }
- public String getFileExt()
- {
- return m_fileExt;
- }
- public String getFileName()
- {
- return m_filename;
- }
- public String getFilePathName()
- {
- return m_filePathName;
- }
- public int getSize()
- {
- return m_size;
- }
- protected int getStartData()
- {
- return m_startData;
- }
- public String getSubTypeMIME()
- {
- return m_subTypeMime;
- }
- public String getTypeMIME() throws IOException
- {
- return m_typeMime;
- }
- public boolean isMissing()
- {
- return m_isMissing;
- }
- public void saveAs(String destFilePathName)
- throws SmartUploadException, IOException
- {
- saveAs(destFilePathName, 0);
- }
- public void saveAs(String destFilePathName, int optionSaveAs)
- throws SmartUploadException, IOException
- {
- String path = new String();
- path = m_parent.getPhysicalPath(destFilePathName, optionSaveAs);
- if (path == null)
- throw new IllegalArgumentException(
- "There is no specified destination file (1140).");
- try
- {
- java.io.File file = new java.io.File(path);
- FileOutputStream fileOut = new FileOutputStream(file);
- fileOut.write(m_parent.m_binArray, m_startData, m_size);
- fileOut.close();
- }
- catch (IOException e)
- {
- throw new SmartUploadException("File can't be saved (1120).");
- }
- }
- protected void setContentDisp(String contentDisp)
- {
- m_contentDisp = contentDisp;
- }
- protected void setContentType(String contentType)
- {
- m_contentType = contentType;
- }
- protected void setEndData(int endData)
- {
- m_endData = endData;
- }
- protected void setFieldName(String fieldName)
- {
- m_fieldname = fieldName;
- }
- protected void setFileExt(String fileExt)
- {
- m_fileExt = fileExt;
- }
- protected void setFileName(String fileName)
- {
- m_filename = fileName;
- }
- protected void setFilePathName(String filePathName)
- {
- m_filePathName = filePathName;
- }
- protected void setIsMissing(boolean isMissing)
- {
- m_isMissing = isMissing;
- }
- protected void setParent(SmartUpload parent)
- {
- m_parent = parent;
- }
- protected void setSize(int size)
- {
- m_size = size;
- }
- protected void setStartData(int startData)
- {
- m_startData = startData;
- }
- protected void setSubTypeMIME(String subTypeMime)
- {
- m_subTypeMime = subTypeMime;
- }
- protected void setTypeMIME(String TypeMime)
- {
- m_typeMime = TypeMime;
- }
- }