当前位置:首页 » 文件管理 » struts2上传文件进度条

struts2上传文件进度条

发布时间: 2022-08-19 17:01:21

A. jquery 进度条 struts2

请按照标准的W3C书写

B. 100分。struts2实现的图片上传 例子

这个就是struts2的,我艹
-------------已在linux服务上线---------------------
private File userfile;
private String userfileContentType;
private String userfileFileName;
//get set 略
public void uploadImg() throws Exception {
Person person = factory.getUserServiceImpl().getCurrentPerson();//得到当前用户
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
Calendar cal = Calendar.getInstance();
int yy = cal.get(Calendar.YEAR);
String savePath = request.getSession().getServletContext().getRealPath("/")+"/upload/images/"+yy+"/";
String filePath = savePath ;
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = userfileFileName;
String basePath = "upload/images/"+yy+"/";
String uuid = UUID.randomUUID().toString();
String fileType=fileName.substring(fileName.length()-3);
File uploadedFile = new File(filePath + uuid+"."+fileType);
//userfile.write(uploadedFile);
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(userfile), 1024*1024*10);
out = new BufferedOutputStream(new FileOutputStream(uploadedFile),
1024*1024*10);
byte[] buffer = new byte[1024*1024*10];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

response.setContentType("text/html;charaset=utf-8");
response.getWriter().write("{success:'true',fileURL:'"+basePath + uuid+"."+fileType + "'}");
}

C. struts2如何同时上传文件以及获得表单数据

事实上这根本不需要什么其他配置操作,因为这是Struts2,而不是原生Servlet,在Struts2中,拦截器会将request中的表单数据(或者文件格式的数据)都和action类中的属性名称一一对应的注入值(包括文件数据)。所以你需要做的,其实只是在jsp页面(或html)中加入一个file类型的input标签,名称记住(比如为photo),然后在action类中加一个File类型(java.io.File)字段,此字段必须和刚刚的input标签name属性一致,即photo(private File photo;)。最后,需要注意的是,当你妄图从网页上传一个文件类型的表单时,必须将包围它的form类将enctype="multipart/form-data" method="post"加上,即method必须为post,且enctype,也就是表单数据类型,必须为二进制的。

D. 关于struts2框架的文件上传问题。。。上传的文件超过2MB就报下面的异常,请问怎么解决

在struts.xml中设置
<constant name="struts.multipart.maxSize" value="314572800"></constant> <!-- 允许300M -->
可以允许上传300M的呢!我试了下,上传了个202M的电影,竟然上传成功了!

E. struts2文件上传有没有最大限制

Struts2上传文件的默认大小限制是2M(2097152字节),也可以通过struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上传文件的总大小不能超过2K字节。

struts2上传非常方便了,没听说网上有很多上传插件,上传核心代码在后台,看你怎么获取上传的文件数据怎么将数据再写入到本地服务器。

F. struts2中文件上传问题

你要学会用debug模式来调试自己的代码。把断点设置在 String root = ServletActionContext.getRequest().getRealPath("/upload"); 可以一步步查看每个变量和属性的值 ,这样更容易找到问题。
这里你需要查看root 和 destfile的值 这样很容易看出你得到的路径是否为你想要设置的路劲。
也可以用system.out.println(root) 打印到控制台看一下。

G. 我用struts2做文件上传,传到服务器的始终是tmp临时文件,我想要真正的文件,请怎么解决

struts只能帮你上传成文件流,你的action有个file吧,要想生成真正地文件,还得把File装换成FileOutputStream,然后输出到你真正的文件

H. 谁有带进度条的文件上传项目,java的,基于Struts2的。

flash神马的最讨厌。
请使用jquery的AJAX 那个so so easy

I. java web断点续传,我用的是fileupload来做的上传。

使用Struts2上传文件:

Struts文件上传需要使用File Upload Filter。Filter Upload Filter使用一些默认的规则:

Form中的<s:file name="image"></s:file>标签对应着Action类中的三个属性分别是:上传文件(java.io.File类型),文件名(java.lang.String类型),文件类型(java.lang.String类型,例如:image/jpeg)。命名规约为:

文件:名字与<s:file>标签中的name属性一致,这里为:image

文件名:文件 + FileName,这里为:imageFileName

文件类型:文件 + ContentType,这里为:imageContentType

所以针对上述<s:file name="image"></s:file>表示啊的上传文件的JSP和Action类被别为:

imageUpload.jsp:

[html]view plain

  • <%@pagecontentType="text/html;charset=UTF-8"language="java"%>

  • <%@taglibprefix="s"uri="/struts-tags"%>

  • <html>

  • <head><title>ImageUpload</title></head>

  • <body>

  • <h1>ImageUploadPage</h1>

  • <s:formaction="imageUpload"method="post"enctype="multipart/form-data">

  • <s:filename="image"></s:file>

  • <s:submit></s:submit>

  • </s:form>

  • </body>

  • </html>



  • ImageUploadAction.java:

    [html]view plain

  • packagecom.jpleasure;

  • importcom.opensymphony.xwork2.ActionSupport;

  • importjava.io.File;

  • importjava.io.InputStream;

  • importjava.io.FileInputStream;

  • importjava.io.FileNotFoundException;

  • {

  • privateFileimage;

  • privateStringimageFileName;

  • privateStringimageContentType;

  • publicFilegetImage(){

  • returnimage;

  • }

  • publicvoidsetImage(Fileimage){

  • this.image=image;

  • }

  • publicStringgetImageFileName(){

  • returnimageFileName;

  • }

  • publicvoidsetImageFileName(StringimageFileName){

  • this.imageFileName=imageFileName;

  • }

  • (){

  • returnimageContentType;

  • }

  • publicvoidsetImageContentType(StringimageContentType){

  • this.imageContentType=imageContentType;

  • }

  • publicStringexecute(){

  • if(image!=null){

  • System.out.println("filenameis:"+this.imageFileName);

  • System.out.println("filecontenttypeis:"+this.imageContentType);

  • System.out.println("filelengthis:"+this.image.length());

  • }

  • returnSUCCESS;

  • }

  • }



  • Struts.xml配置文件:

    [html]view plain

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <result>/success.jsp</result>

  • </action>

  • 这样当我们选中上传文件,提交的时候:文件内容会以File类型的方式放在image声明的变量中。文件的名字将会被放在imageFileName命名的变量中,文件的类型被放在imageContentType命名的变量中。

    文件下载:

    文件下载需要使用一个特殊的Result,stream类型的Result。Stream类型的Result主要用来处理文件下载操作。

    处理原理为:所有的下载文件都是将一个二进制的流写入到HttpResponse中去。在Action类中定义一个InputSream类型的二进制流,在Result返回给用户的时候返回给用户。

    扩展上述的代码,将上传来的文件直接下载给用户:

    ImageUploadAction中需要追加一个InputSream类型的对象,并且指向上传的文件,代码如下,红色部分表示变化:

    [html]view plain

  • packagecom.jpleasure;

  • importcom.opensymphony.xwork2.ActionSupport;

  • importjava.io.File;

  • importjava.io.InputStream;

  • importjava.io.FileInputStream;

  • importjava.io.FileNotFoundException;

  • {

  • privateFileimage;

  • privateStringimageFileName;

  • privateStringimageContentType;

  • =null;

  • (){

  • returnimageInputStream;

  • }

  • publicvoidsetImageInputStream(InputStreamimageInputStream){

  • this.imageInputStream=imageInputStream;

  • }

  • publicFilegetImage(){

  • returnimage;

  • }

  • publicvoidsetImage(Fileimage){

  • this.image=image;

  • }

  • publicStringgetImageFileName(){

  • returnimageFileName;

  • }

  • publicvoidsetImageFileName(StringimageFileName){

  • this.imageFileName=imageFileName;

  • }

  • (){

  • returnimageContentType;

  • }

  • publicvoidsetImageContentType(StringimageContentType){

  • this.imageContentType=imageContentType;

  • }

  • publicStringexecute(){

  • if(image!=null){

  • System.out.println("filenameis:"+this.imageFileName);

  • System.out.println("filecontenttypeis:"+this.imageContentType);

  • System.out.println("filelengthis:"+this.image.length());

  • try{

  • this.imageInputStream=newFileInputStream(image);

  • }catch(FileNotFoundExceptione){

  • e.printStackTrace();

  • }

  • }

  • returnSUCCESS;

  • }

  • }



  • 配置文件为,红色为变化部分:

    [html]view plain

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <resultname="success"type="stream">

  • <paramname="contentType">image/pjpeg</param>

  • <paramname="inputName">imageInputStream</param>

  • <paramname="contentDisposition">attachment;filename="image.jpg"</param>

  • <paramname="bufferSize">1024</param>

  • </result>

  • </action>



  • ContentType表示下载文件的类型。

    InputName表示Action类中用来下载文件的字段的名字。

    ContentDisposition用来控制文件下载的一些信息,包括是否打开另存对话框,下载文件名等。

    BufferSize表示文件下载时使用的缓冲区的大小。

    实际项目开发的考虑:

    控制上传文件的类型和最大允许上传文件的size

    使用File Upload Intercepter的参数可盈控制上传文件的类型和最大允许上传文件的size。例如:

    [html]view plain

  • <struts>

  • <packagename="myPackage"extends="struts-default">

  • <interceptor-refname="fileUpload">

  • <paramname="maximumSize">2MB</param>

  • <paramname="allowedTypes">text/html,image/jpeg</param>

  • </interceptor-ref>

  • <interceptor-refname="basicStack"/>

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <resultname="success"type="stream">

  • <paramname="contentType">image/pjpeg</param>

  • <paramname="inputName">imageInputStream</param>

  • <paramname="contentDisposition">

  • attachment;filename="image.jpg"

  • </param>

  • <paramname="bufferSize">1024</param>

  • </result>

  • </action>

  • </package>

  • </struts>



  • 上述表示允许上传jpeg和html类型的文件,且最大文件上传size为2MB

    显示错误信息:

    可以使用如下key表示的message来显示文件上传出错的提示信息:

    消息Key 说明

    struts.messages.error.uploading 文件无法正常上传时的公共错误

    struts.messages.error.file.too.large 文件大小超过最大允许size时的错误提示

    struts.messages.error.content.type.not.allowed 文件类型不在上传文件允许类型中的错误提示

J. struts2 如何实现上传整个文件夹的功能

一、压缩文件其实是可以0压缩率直接打包,这样其实蛮快的
二、看到网上说Applet可以上传文件夹,具体远离不清楚,你可以看看
三、最笨的方法,用Ajax做一个递归遍历文件夹的函数,如果是文件就上传上去,如果是文件夹就请求后台新建文件夹
四、用JSON格式把目录和文件封装起来,统一传到后台,但是后台处理要比较麻烦

热点内容
san存储和nas存储 发布:2025-05-14 04:34:44 浏览:151
幽灵战士3什么配置 发布:2025-05-14 04:33:53 浏览:113
安卓的虚拟机哪个好用 发布:2025-05-14 04:32:34 浏览:870
宿迁存储式化工设备 发布:2025-05-14 04:32:33 浏览:53
s7200编程s7200 发布:2025-05-14 04:28:32 浏览:413
安卓定制版苹果手机是什么意思 发布:2025-05-14 04:26:27 浏览:379
如何搭建php环境虚拟服务器免费 发布:2025-05-14 04:25:37 浏览:103
相册加密怎么看 发布:2025-05-14 04:24:53 浏览:573
怎么压缩邮件 发布:2025-05-14 04:16:51 浏览:497
云服务器搭建邮箱绑定郁闷 发布:2025-05-14 04:16:48 浏览:149