首页 > 编程知识 正文

java大文件上传解决方案,javaweb书籍推荐

时间:2023-05-06 04:00:40 阅读:49114 作者:1142

有关上载文件的信息,请执行以下操作:

部署commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar两个软件包

将表单更改为上载文件模式: enctype='multipart/form-data '

3 .开始编写相关代码

这里是磁盘文件工厂DiskFileItemFactory; 创建servlet文件上传类: servlet文件upload

有几种重要的方法。 DiskFileItemFactory类用于转移目录的方法setRepository,该方法作为临时文件存储在磁盘上。

ServletFileUpload类检索表单中的所有数据,并检索表单中的所有元素方法。 永久请求

看看具体的代码吧。

表示以这种方式上传的文件保存在服务器端!

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.UUID;

导入javax.servlet.servlet exception;

导入javax.servlet.http.http servlet;

import javax.servlet.http.http servlet请求;

import javax.servlet.http.http无servlet保罗;

importorg.Apache.com mons.file upload.fileitem;

importorg.Apache.com mons.file upload.file upload exception;

importorg.Apache.com mons.file upload.disk.diskfileitemfactory;

importorg.Apache.com mons.file upload.servlet.servlet file upload;

importorg.Apache.com mons.io.fileutils;

publicclassuploadservletextendshttp servlet {

//*

*构造器of the object。

*/

公共上载servlet {

super (;

}

//*

* destruction of the servlet。

*/

公共语音目录

super.destroy (; //Just puts 'destroy' string in log

//Put your code here

}

//*

* thedogetmethodofthe servlet。

*

* thismethodiscalledwhenaformhasitstagvaluemethodequalstoget。

*

* @ paramrequesttherequestsendbytheclienttotheserver

* @ paramresponsetheresponsesendbytheservertotheclient

* @ throwsservletexceptionifanerroroccurred

* @ throwsioexceptionifanerroroccurred

*/

公共语音文档(httpservletrequestrequest,http servlet响应) )。

throws ServletException,IOException {

this.dopost (请求,响应);

}

//*

* thedopostmethodofthe servlet。

*

* thismethodiscalledwhenaformhasitstagvaluemethodequalstopost。

*

* @ paramrequesttherequestsendbytheclienttotheserver

* @ paramresponsetheresponsesendbytheservertotheclient

* @ throwsservletexceptionifanerroroccurred

* @ throwsioexceptionifanerroroccurred

*/

公共语音文档(httpservletrequestrequest,http服务器响应响应) )。

throws ServletException,IOException {

response.set content type (' text/html );

printwriter out=response.get writer (;

diskfileitemfactorysf=newdiskfileitemfactory (; //实例化磁盘在文件列表工厂

string path=request.get realpath (/upload ); //获取上载文件的存储目录

SF.set repository (新文件) path ); //设置文件存储目录

SF.setsizethreshold(1024*1024 ); //设定文件的上传不足1M,放置在内存中

字符串名称=' '; //文件新生成的文件名

字符串文件名=' '; //文件的原名

String name=' '; //普通字段

//从工厂获取servlet的upload文件上传类

servletfileuploadsfu=newservletfileupload (SF;

try {

列表lst=sfu.parse request (request ); 获得请求的所有元素

for(fileitemfileitem:lst ) {

if(fileitem.isformfield () ) ) )。

if (' name '.equals (fileitem.getfield name ) ) ) ) ) )。

name=fileitem.getstring(utf-8 );

}

}else{

获取//文件名

fileName=fileItem.getName (;

filename=filename.substring (filename.lastindexof ('' )1);

stringhouzhui=filename.substring (filename.lastindexof ('.' ) );

rename=UUID.randomUUID () houzhui;

fileitem.write(newfile ) path,rename );

}

}

}catch(exceptione ) {

//todo auto-generated catch块

e .打印堆栈跟踪(;

}

System.out.println (常规字段) name;

System.out.println (文件名) fileName;

System.out.println ('修改后生成的文件名' rename );

response.sendredirect('ok.JSP );

out.flush (;

out.close (;

}

//*

*初始化ofthe servlet。

*

* @ throwsservletexceptionifanerroroccurs

*/

公共void init () throws ServletException {

//Put your code here

}

}

index.jsp页面:

测试文件接口名称:

图:

确定. JSP页面:

文件上传成功! 要达到效果就不拍截图。 需要在自己的过去使用!

以上java文件已上传到Demo (必看篇),是编辑与大家共享的全部内容。 希望能作为参考。 另外,请支持编剧。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。