我在用apache common-io jar包作影片上传时,发现大文件上传出错,以此博客作为笔记。调试并研究后发现,导致问题的原因是
byte[] fileData = new byte[(int)this.getSize()];
当影片size长度大于2G则会强转失败,故作了如下修改:
package org.apache.commons.fileupload.disk; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemHeaders; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.ParameterParser; import org.apache.commons.fileupload.util.Streams; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.DeferredFileOutputStream; import java.io.*; import java.util.Map; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; /** * Created by lucky god on 2017/4/24. */ 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; 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.contentType = contentType; this.isFormField = isFormField; this.fileName = fileName; this.sizeThreshold = sizeThreshold; this.repository = repository; } public InputStream getInputStream() throws IOException { if(!this.isInMemory()) { return new FileInputStream(this.dfos.getFile()); } else { if(this.cachedContent == null) { this.cachedContent = this.dfos.getData(); } return new ByteArrayInputStream(this.cachedContent); } } public String getContentType() { return this.contentType; } public String getCharSet() { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); Map params = parser.parse(this.getContentType(), ';'); return (String)params.get("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)this.dfos.getData().length:this.dfos.getFile().length())); } public byte[] get() { if(this.isInMemory()) { if(this.cachedContent == null) { this.cachedContent = this.dfos.getData(); } return this.cachedContent; } else { if (this.getSize() > Integer.MAX_VALUE){ return null; } byte[] fileData = new byte[(int)this.getSize()]; BufferedInputStream fis = null; try { fis = new BufferedInputStream(new FileInputStream(this.dfos.getFile())); fis.read(fileData); } 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 String(this.get(), charset); } public String getString() { byte[] rawdata = this.get(); String charset = this.getCharSet(); 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; BufferedInputStream fis = null; try { outputFile = new FileOutputStream(file); // outputFile.write(this.get()); if (this.getSize() > Integer.MAX_VALUE){ byte[] temp = new byte[100*1024*1024]; //100M int len; fis = new BufferedInputStream(new FileInputStream(this.dfos.getFile())); while ((len = fis.read(temp)) != -1){ outputFile.write(temp,0, len); outputFile.flush(); } } else{ outputFile.write(this.get()); } } finally { if(outputFile != null) { outputFile.close(); } if(fis != null) { fis.close(); } } } else { File outputFile1 = this.getStoreLocation(); if(outputFile1 == null) { throw new FileUploadException("Cannot write uploaded file to disk!"); } this.size = outputFile1.length(); if(!outputFile1.renameTo(file)) { BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(outputFile1)); out = new BufferedOutputStream(new FileOutputStream(file)); IOUtils.copy(in, out); } 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 = this.getStoreLocation(); if(outputFile != null && outputFile.exists()) { outputFile.delete(); } } public String getFieldName() { return this.fieldName; } public void setFieldName(String fieldName) { this.fieldName = fieldName; } public boolean isFormField() { return this.isFormField; } public void setFormField(boolean state) { this.isFormField = state; } public OutputStream getOutputStream() throws IOException { if(this.dfos == null) { File outputFile = this.getTempFile(); this.dfos = new DeferredFileOutputStream(this.sizeThreshold, outputFile); } return this.dfos; } public File getStoreLocation() { return this.dfos == null?null:this.dfos.getFile(); } protected void finalize() { File outputFile = this.dfos.getFile(); if(outputFile != null && outputFile.exists()) { outputFile.delete(); } } protected File getTempFile() { if(this.tempFile == null) { File tempDir = this.repository; if(tempDir == null) { tempDir = new File(System.getProperty("java.io.tmpdir")); } String tempFileName = String.format("upload_%s_%s.tmp", new Object[]{UID, getUniqueId()}); this.tempFile = new File(tempDir, tempFileName); } return this.tempFile; } private static String getUniqueId() { int limit = 100000000; int current = COUNTER.getAndIncrement(); String id = Integer.toString(current); if(current < 100000000) { id = ("00000000" + id).substring(id.length()); } return id; } public String toString() { return String.format("name=%s, StoreLocation=%s, size=%s bytes, isFormField=%s, FieldName=%s", new Object[]{this.getName(), this.getStoreLocation(), Long.valueOf(this.getSize()), Boolean.valueOf(this.isFormField()), this.getFieldName()}); } private void writeObject(ObjectOutputStream out) throws IOException { if(this.dfos.isInMemory()) { this.cachedContent = this.get(); } else { this.cachedContent = null; this.dfosFile = this.dfos.getFile(); } out.defaultWriteObject(); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if(this.repository != null) { if(!this.repository.isDirectory()) { throw new IOException(String.format("The repository [%s] is not a directory", new Object[]{this.repository.getAbsolutePath()})); } if(this.repository.getPath().contains("\u0000")) { throw new IOException(String.format("The repository [%s] contains a null character", new Object[]{this.repository.getPath()})); } } OutputStream output = this.getOutputStream(); if(this.cachedContent != null) { output.write(this.cachedContent); } else { FileInputStream input = new FileInputStream(this.dfosFile); IOUtils.copy(input, output); this.dfosFile.delete(); this.dfosFile = null; } output.close(); this.cachedContent = null; } public FileItemHeaders getHeaders() { return this.headers; } public void setHeaders(FileItemHeaders pHeaders) { this.headers = pHeaders; } }
对应包位置应与原位置一致,博文中包目录不变。
附文件下载
相关推荐
Apache Commons IO 2.5 (要求 JDK 1.6),IOUtils,FileUtils,jar包下载
apache-skywalking-java-agent-8.9.0 主要监控 jvm 服务
Java_apache-maven-3.3.1.rar_安装包_kaic Java_apache-maven-3.3.1.rar_安装包_kaic Java_apache-maven-3.3.1.rar_安装包_kaic Java_apache-maven-3.3.1.rar_安装包_kaic Java_apache-maven-3.3.1.rar_安装包_kaic ...
Java_apache-tomcat-9.0.0.M27x64安装包_kaic.rar Java_apache-tomcat-9.0.0.M27x64安装包_kaic.rar Java_apache-tomcat-9.0.0.M27x64安装包_kaic.rar Java_apache-tomcat-9.0.0.M27x64安装包_kaic.rar Java_apache-...
离线安装包,亲测可用
apache-skywalking-java-agent-8.8.0.tgz apache-skywalking-java-agent-8.9.0.tgz apache-skywalking-java-agent-8.10.0.tgz apache-skywalking-java-agent-8.11.0.tgz apache-skywalking-java-agent-8.12.0.tgz ...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
commons-io-2.6.jar下载
用于压缩/解压缩的java开发工具包,基本上主流格式全包含,其中apache-common-compress内有5个jar包,两个是test的不用管,剩余三个,一个是源码包,一个是开发包另一个是javadoc。解压除RAR外的所有格式。 apache-...
apache-jakarta旗下的所有开源项目jar文件 apache-jakarta-commons-codec.jar apache-jakarta-commons-collections.jar apache-jakarta-commons-discovery.jar apache-jakarta-commons-email.jar apache-jakarta-...
主要介绍了Java避免UTF-8的csv文件打开中文出现乱码的方法,结合实例形式分析了java操作csv文件时使用utf-16le编码与utf8编码相关操作技巧,需要的朋友可以参考下
Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,包含了最新的commons-io-2.0.1-bin,commons-io-2.0.1-src,commons-io-2.0.1-doc
commons-io 是 Apache 开源基金组织提供的一组有关 IO 操作的类库,可以挺提高 IO 功能开发的效率。commons-io 工具包提供了很多有关 IO 操作的类
常用的JAVA,Android包库,闲的整理一下打包上传,math3主要用于线性计算,回归分析等,jama主要用于矩阵计算,Poi用于输出大量数据到Excel,再导入matlab。commonIO也是用于输入输出流
用开源的FileUpload组件,可以很方便的给自己的系统添加功能强大的文件上传模块。 该资源文件是两个jar打包成的压缩文件。
NULL 博文链接:https://yangyangmyself.iteye.com/blog/1299997
commons-io-2.5 <groupId>org.apache.commons <artifactId>commons-parent <version>39 <modelVersion>4.0.0 <groupId>commons-io <artifactId>commons-io ... <name>Apache Commons IO</name>
标签:server、codec、apache、apacheds、kerberos、directory、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...
commons-io-2.11.0.rar