发布于 2018-02-12 06:25:45 | 227 次阅读 | 评论: 0 | 来源: 网友投递
Java程序设计语言
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
文件上传
multipart/form-data
<input type=“file”>
字段.
<s:form action="testUpload" enctype="multipart/form-data">
<s:textfield name="userName[0]" label="用户-1"></s:textfield>
<s:file name="photos" label="照片"></s:file>
<s:textfield name="userName[1]" label="用户-2"></s:textfield>
<s:file name="photos" label="照片"></s:file>
<s:textfield name="userName[2]" label="用户-3"></s:textfield>
<s:file name="photos" label="照片"></s:file>
<s:submit value="提交"></s:submit>
</s:form>
public class UploadAction extends ActionSupport{
@Setter@Getter
private List<File> photos;
@Setter@Getter
private List<String> photosContentType;
@Setter@Getter
private List<String> photosFileName;
@Setter@Getter
private List<String> userName;
public String testUpload() throws IOException {
System.out.println("userName: "+userName);
System.out.println("photos: "+photos);
System.out.println("photosFileName: "+ photosFileName);
System.out.println("photosContentType: "+photosContentType);
// 将文件传到服务器根目录下upload文件下
// 获取ServletContext
ServletContext servletContext = ServletActionContext.getServletContext();
//获取真实路径
String realPath = servletContext.getRealPath("/upload");
System.out.println(realPath);
File uploadFile = new File(realPath);
//判断路径是否存在
if (!uploadFile.exists()){
//不存在创建
uploadFile.mkdir();
}
for (int i = 0; i < photos.size(); i++) {
UUID uuid = UUID.randomUUID();
FileUtils.copyFile(photos.get(i), new File(realPath + "/" + uuid + photosFileName.get(i)));
}
return SUCCESS;
}
}
1.处理几个小问题?
1.文件名重名,一般可以在文件名的前面生成一个UUID作为前缀。
2.限制单个文件的大小
3.限制文件的类型
4.限制总文件的大小
2.在Struts2 中提供了 FileUpload 拦截器 可以给我们设置这些属性值
FileUpload 拦截器有 3 个属性可以设置.
注意: 在 org.apache.struts2 下的 default.properties 中有对上传的文件总的大小的限制. 可以使用常量的方式来修改该限制struts.multipart.maxSize=2097152
<constant name="struts.devMode" value="true"/>
<!-- 在这修改总文件的的大小 -->
<constant name="struts.multipart.maxSize" value="2097152"/>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="defaultStack">
<!-- 修改单个文件大小,Commons FileUpload 组件默认接受上传文件总的最大值为 2M -->
<param name="fileUpload.maximumSize">57,408</param>
<!-- 允许上传的文件类型 -->
<param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
<!-- 允许上传文件的扩展名 -->
<param name="fileUpload.allowedExtensions">jpg,gif</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myInterceptor"></default-interceptor-ref>
<action name="testUpload" class="org.pan.action.UploadAction" method="testUpload">
<result name="success">/WEB-INF/views/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
一.上传文件相关的错误消息?
1.与文件上传有关的出错消息在 struts-messages.properties 文件里预定义.
2.可以在文件上传 Action 相对应的资源文件 或者 在 i18n_zh_CN.properties 国际化资源文件中重新定义错误消息
struts.messages.error.file.too.large=你传的文件太大了
struts.messages.error.content.type.not.allowed=文件类型错误
struts.messages.error.file.extension.not.allowed=扩展名错误
struts.messages.upload.error.SizeLimitExceededException=文件总大小超过上限
文件下载
在某些应用程序里, 可能需要动态地把一个文件发送到用户的浏览器中, 而这个文件的名字和存放位置在编程时是无法预知的
示例代码
<a href="testDownLoad">下载</a>
public class DownLoadAction extends ActionSupport{
//通常以下这几个参数会在Action 中提供
@Setter@Getter
private String contentType;
@Setter@Getter
private long contentLength;
@Setter@Getter
private String contentDisposition;
@Setter@Getter
private InputStream inputStream;
public String testDownLoad() throws FileNotFoundException, UnsupportedEncodingException {
//获取ServletContext
ServletContext servletContext = ServletActionContext.getServletContext();
//获取文件的路径
String realPath = servletContext.getRealPath("/WEB-INF/file/至少还有你.mp3");
//获取文件的流
inputStream = new FileInputStream(realPath);
//设置文件的类型
contentType = servletContext.getMimeType(realPath);
//获取文件的长度
contentLength = new File(realPath).length();
//设置文件名
String fileName = "至少还有你.mp3";
fileName = new String(fileName.getBytes("gbk"),"iso8859-1");
contentDisposition = "attachment;filename="+fileName;
return SUCCESS;
}
}
<!-- 文件下载 -->
<action name="testDownLoad" class="org.pan.action.DownLoadAction" method="testDownLoad">
<result type="stream">
<!-- 文件缓冲大小 -->
<param name="bufferSize">2048</param>
</result>
</action>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。