导航:首页 > 版本升级 > strtus2文件上传代码获取参数

strtus2文件上传代码获取参数

发布时间:2024-10-12 17:36:33

㈠ 使用struts2 怎么实现 文件上传到另外一台电脑

传输文件的功能,类似于网络聊天程序

肯定要用的文件传输用的IO流,还有网络专通信用到属的socket了。

可以在你部署struts2网站的服务器上面写一个网络通信程序(服务器端),对应的网络通信程序(客户端)放在“另外一台电脑”上面。

网站的服务器启动之后:
1。把那个网络通信程序(服务器端)启动

2。把“另外一台电脑”上面的网络通信程序(客户端)启动,现在两端就建立连接了。

3。可以通过服务器端向客户端发送数据。

以上过程跟我们平时用的聊天程序一样。

你可以在网上看看相应的网络聊天程序,现在网上这样的程序还是很多的。
里面实现了这样的机制。

㈡ 用java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv

package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 完成文件上传 (不是解析上传内容,因为上传内容 由fileUpload拦截器负责解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上传内容
// <input type="file" name="upload" />
private File upload; // 这里变量名 和 页面表单元素 name 属性一致
private String uploadContentType;
private String uploadFileName;

public void setUpload(File upload) {
this.upload = upload;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
if (upload == null) { // 通过xml配置 required校验器 完成校验
// 没有上传文件
return NONE;
}
// 将上传文件 保存到服务器端
// 源文件 upload
// 目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + uploadFileName);
// 文件复制 使用commons-io包 提供 工具
FileUtils.File(upload, destFile);
return NONE;
}
}
多文件上传
package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 支持多文件上传
*
* @author seawind
*
*/
public class MultiUploadAction extends ActionSupport {
// 接收多文件上传参数,提供数组接收就可以了
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;

public void setUpload(File[] upload) {
this.upload = upload;
}

public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
for (int i = 0; i < upload.length; i++) {
// 循环完成上传
File srcFile = upload[i];
String filename = uploadFileName[i];

// 定义目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.File(srcFile, destFile);
}
return NONE;
}
}

㈢ 为什么Struts2上传文件之后扩展名变成了tmp,如何取得文件正确的扩展名

在action中获取到上传文件名利用上传文件名来存储就不会出现tmp结尾文件了

㈣ struts2上传文件的类型去哪里找

struts2是根据contentType来限制的,并不是文件的扩展名

比如我想仅上传image/png,image/gif,image/jpeg这三种文件类型

第一种方法是通过javascript校验来限制,这个比较简单,获取input的value然后截取扩展名进行判断即可

第二种是根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制,步骤如下:

1 配置fileupload拦截器

struts2的defaultStack中已经含有fileupload拦截器,如果想加入allowedTypes参数,需要从新写一个defaultstack ,拷贝过来修改一下即可:

<interceptor-stack name="myDefaultStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="debugging"/>

<interceptor-ref name="profiling"/>

<interceptor-ref name="scopedModelDriven"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload">

<param name="allowedTypes">

image/png,image/gif,image/jpeg

</param>

</interceptor-ref>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="actionMappingParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>

</interceptors>

<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>

仅修改代码中的

<interceptor-ref name="fileUpload">

<param name="allowedTypes">

image/png,image/gif,image/jpeg

</param>

</interceptor-ref>

上面配置的是上传文件类型的限制,其实共有两个参数

maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位). 注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB

allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html),这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.

2 jsp页面定义如下(testFileUpload.jsp)

<s:form action="testFileUpload" method="post" enctype="multipart/form-data">

<s:file name="file"theme="simple"/>

<s:fielderror name="file"></s:fielderror>

<s:submit/>

</s:form>

3 后台的action声明如下(我用的是struts2的注解进行action配置)

public class TestFileUploadAction extends ActionSupport{

private File file;

private String fileContentType;

private String fileFileName;

@Action(

value = "testFileUpload", results = {

@Result(name = "input", location = "/testFileUpload.jsp"),

@Result(name = "success", location = "/testFileUploadSuccess.jsp")

}

)

public String execute() {

return SUCCESS;

}

get/set......

}

注意:如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为

private File xxx;

private String xxxContentType;

private String xxxFileName;

同时注意大小写一定要一致

4 定义错误文件类型的消息提示,这个需要用到struts2的资源文件,在struts.properties文件中加入

struts.custom.i18n.resources=globalMessages

globalMessages对应着资源文件名

5 在源文件夹下定义资源文件globalMessages.properties,并在里面加入如下信息:

struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate

这里稍作说明(拷贝一下struts2的帮助):

如果你的action实现了ValidationAware接口(如果action继承了ActionSupport,那么就相当于实现了ValidationAware),这个拦截器就可以添加几种字段错误.这些错误信息是基于存储在struts-messages.properties文件中的一些i18n值,这个文件是所有i18n请求的默认文件.你可以在自己消息文件的复写以下key的消息文字

struts.messages.error.uploading - 文件不能上传的通用错误信息

struts.messages.error.file.too.large - 上传文件长度过大的错误信息

struts.messages.error.content.type.not.allowed - 当上传文件不符合指定的contentType

以上配置完毕后,测试一下,对于非法的contentType,例如xxx.log这个文件的的contentType是pplication/octet-stream

会给出提示:upload file contenttype is invalidate

'.a' : 'application/octet-stream',

'.ai' : 'application/postscript',

'.aif' : 'audio/x-aiff',

'.aifc' : 'audio/x-aiff',

'.aiff' : 'audio/x-aiff',

'.au' : 'audio/basic',

'.avi' : 'video/x-msvideo',

'.bat' : 'text/plain',

'.bcpio' : 'application/x-bcpio',

'.bin' : 'application/octet-stream',

'.bmp' : 'image/x-ms-bmp',

'.c' : 'text/plain',

'.cdf' : 'application/x-cdf',

'.cdf' : 'application/x-netcdf',

'.cpio' : 'application/x-cpio',

'.csh' : 'application/x-csh',

'.css' : 'text/css',

'.dll' : 'application/octet-stream',

'.doc' : 'application/msword',

'.dot' : 'application/msword',

'.dvi' : 'application/x-dvi',

'.eml' : 'message/rfc822',

'.eps' : 'application/postscript',

'.etx' : 'text/x-setext',

'.exe' : 'application/octet-stream',

'.gif' : 'image/gif',

'.gtar' : 'application/x-gtar',

'.h' : 'text/plain',

'.hdf' : 'application/x-hdf',

'.htm' : 'text/html',

'.html' : 'text/html',

'.ief' : 'image/ief',

'.jpe' : 'image/jpeg',

'.jpeg' : 'image/jpeg',

'.jpg' : 'image/jpeg',

'.js' : 'application/x-javascript',

'.ksh' : 'text/plain',

'.latex' : 'application/x-latex',

'.m1v' : 'video/mpeg',

'.man' : 'application/x-troff-man',

'.me' : 'application/x-troff-me',

'.mht' : 'message/rfc822',

'.mhtml' : 'message/rfc822',

'.mif' : 'application/x-mif',

'.mov' : 'video/quicktime',

'.movie' : 'video/x-sgi-movie',

'.mp2' : 'audio/mpeg',

'.mp3' : 'audio/mpeg',

'.mpa' : 'video/mpeg',

'.mpe' : 'video/mpeg',

'.mpeg' : 'video/mpeg',

'.mpg' : 'video/mpeg',

'.ms' : 'application/x-troff-ms',

'.nc' : 'application/x-netcdf',

'.nws' : 'message/rfc822',

'.o' : 'application/octet-stream',

'.obj' : 'application/octet-stream',

'.oda' : 'application/oda',

'.p12' : 'application/x-pkcs12',

'.p7c' : 'application/pkcs7-mime',

'.pbm' : 'image/x-portable-bitmap',

'.pdf' : 'application/pdf',

'.pfx' : 'application/x-pkcs12',

'.pgm' : 'image/x-portable-graymap',

'.pl' : 'text/plain',

'.png' : 'image/png',

'.pnm' : 'image/x-portable-anymap',

'.pot' : 'application/vnd.ms-powerpoint',

'.ppa' : 'application/vnd.ms-powerpoint',

'.ppm' : 'image/x-portable-pixmap',

'.pps' : 'application/vnd.ms-powerpoint',

'.ppt' : 'application/vnd.ms-powerpoint',

'.ps' : 'application/postscript',

'.pwz' : 'application/vnd.ms-powerpoint',

'.py' : 'text/x-python',

'.pyc' : 'application/x-python-code',

'.pyo' : 'application/x-python-code',

'.qt' : 'video/quicktime',

'.ra' : 'audio/x-pn-realaudio',

'.ram' : 'application/x-pn-realaudio',

'.ras' : 'image/x-cmu-raster',

'.rdf' : 'application/xml',

'.rgb' : 'image/x-rgb',

'.roff' : 'application/x-troff',

'.rtx' : 'text/richtext',

'.sgm' : 'text/x-sgml',

'.sgml' : 'text/x-sgml',

'.sh' : 'application/x-sh',

'.shar' : 'application/x-shar',

'.snd' : 'audio/basic',

'.so' : 'application/octet-stream',

'.src' : 'application/x-wais-source',

'.sv4cpio': 'application/x-sv4cpio',

'.sv4crc' : 'application/x-sv4crc',

'.swf' : 'application/x-shockwave-flash',

'.t' : 'application/x-troff',

'.tar' : 'application/x-tar',

'.tcl' : 'application/x-tcl',

'.tex' : 'application/x-tex',

'.texi' : 'application/x-texinfo',

'.texinfo': 'application/x-texinfo',

'.tif' : 'image/tiff',

'.tiff' : 'image/tiff',

'.tr' : 'application/x-troff',

'.tsv' : 'text/tab-separated-values',

'.txt' : 'text/plain',

'.ustar' : 'application/x-ustar',

'.vcf' : 'text/x-vcard',

'.wav' : 'audio/x-wav',

'.wiz' : 'application/msword',

'.wsdl' : 'application/xml',

'.xbm' : 'image/x-xbitmap',

'.xlb' : 'application/vnd.ms-excel',

'.xls' : 'application/excel',

'.xls' : 'application/vnd.ms-excel',

'.xml' : 'text/xml',

'.xpdl' : 'application/xml',

'.xpm' : 'image/x-xpixmap',

'.xsl' : 'application/xml',

'.xwd' : 'image/x-xwindowmp',

'.zip' : 'application/zip',

㈤ struts2实现文件上传的时候在form表单中设置enctype="multipart/form-data"属性值

SmartUpload su = new SmartUpload();

su.getRequest().getParameter( );

㈥ Struts2怎么获取到 上传文件的源路径

必要前提:

a.表单method必须是post;

b.enctype取值必须是multipart/form-data;

c.提供文件选择域。定义参数接收

File photo; // 接受上传的文件 File接受

String photoFileName; // 上传的文件名 文件name属性+FileName,

String photoContentType; // 文件的MIME类型 文件name属性+ContentType

例如:

action中

㈦ struts2 上传过滤文件类型

在struts.xml中对于一些陌生的类型文件上传,不太确定。我习惯在上传的action中添加方法,判断类型的方法。

//定义文件名称
String fileFileName;
//获取文件类型
String type = fileFileName.substring(fileFileName.lastIndexOf('.') + 1);
//判断文件是否是图片类型
if(type.equals("apk"){
里面写你的内容
}

阅读全文

与strtus2文件上传代码获取参数相关的资料

热点内容
javaswt打印 浏览:787
js套用 浏览:870
缺页中断是属于程序中断 浏览:290
荧光棒可以编程什么有趣的图案 浏览:108
linux所有文件打包压缩 浏览:196
中文系统挂载配置文件 浏览:508
win10要设置什么格式文件 浏览:32
如何修复损坏文件 浏览:369
卖货车在哪个网站 浏览:555
苹果6A158 浏览:264
清除文件内容用什么软件 浏览:19
网上有什么编程课 浏览:689
书链文件名是什么 浏览:828
phpnginx配置文件在哪 浏览:601
iphone6主板焊接技术 浏览:140
win10何设置系统还原 浏览:587
word2010打印背景颜色 浏览:994
河北疫苗app叫什么 浏览:54
win10文件夹显示图片 浏览:967
视频太长微信上发不出怎么办啊 浏览:724

友情链接