1.upload.java(java文件上传的读写方法)
package com.OS.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.sql.bean.FILE; import com.opensymphony.xwork2.ActionSupport; public class upload extends ActionSupport { private File picture; private String pictureContentType; private String pictureFileName; FILE file=new FILE(); public FILE upload() throws Exception{ //最好保护文件时重命名加时间截 File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),pictureFileName); InputStream ins=null; OutputStream ous=null; try{ saved.getParentFile().mkdirs(); ins=new FileInputStream(picture); ous=new FileOutputStream(saved); byte[] b= new byte[1024]; int len =0; while((len=ins.read(b))!=-1){ ous.write(b,0,len); } }catch(Exception e){ e.printStackTrace(); }finally{ if(ous!=null) ous.close(); if(ins!=null) ins.close(); } file.setFile(picture); file.setFileContentType(pictureContentType); file.setFileFileName(pictureFileName); // BufferedReader bf = new BufferedReader(new StringReader(str)); return file; } public File getPicture() { return picture; } public void setPicture(File picture) { this.picture = picture; } public String getPictureContentType() { return pictureContentType; } public void setPictureContentType(String pictureContentType) { this.pictureContentType = pictureContentType; } public String getPictureFileName() { return pictureFileName; } public void setPictureFileName(String pictureFileName) { this.pictureFileName = pictureFileName; } }
2.FILE.java (文件上传类)
package com.sql.bean; import java.io.File; public class FILE { private File file; private String fileContentType; private String fileFileName; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } }
3.uploads.java (action方法)
package com.web.actoin; import org.apache.struts2.ServletActionContext; import com.OS.util.upload; import com.opensymphony.xwork2.ActionSupport; public class uploads extends ActionSupport{ public upload u=new upload(); public String uploadimage() throws Exception{ System.out.println(".."+ServletActionContext.getRequest().getContextPath()+"/upload/"+u.upload().getFileFileName()); return "success"; } public upload getU() { return u; } public void setU(upload u) { this.u = u; } }
4.gMessages.properties(消息文件)
struts.messages.error.uploading = u6587u4EF6u4E0Du80FDu4E0Au4F20u7684u901Au7528u9519u8BEFu4FE1u606F struts.messages.error.file.too.large = u4E0Au4F20u6587u4EF6u957Fu5EA6u8FC7u5927u7684u9519u8BEFu4FE1u606F struts.messages.error.content.type.not.allowed =u5F53u4E0Au4F20u6587u4EF6u4E0Du7B26u5408u6307u5B9Au7684contentType
5.struts.properties(struts2资源文件)
struts.custom.i18n.resources=gMessages
6.struts.xml(struts2配置文件)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="yzm" extends="struts-default"> <action name="uploadimage" class="com.web.actoin.uploads" method="uploadimage"> <interceptor-ref name="fileUpload"> <param name="savePath">/jsp/</param> <!-- 文件过滤 --> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> <!-- 上传文件大小, 以字节为单位 --> <param name="maximumSize">1025956</param> </interceptor-ref> <!-- 默认拦截器必须放在fileUpload之后,否则无效 --> <interceptor-ref name="defaultStack" /> <!-- 异常时返回的界面 --> <result name="input">index.jsp</result> <result name="success">success.jsp</result> </action> </package> </struts>
7.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <base href="<%=basePath%>"> <link href="<%=request.getContextPath() %>/css/init.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" href="<%=path%>/css/exam.css" /> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form action="uploadimage" enctype="multipart/form-data" method="post" theme = "simple"> <s:file name="u.picture" label="上传视频" accept=".bmp,.gif,.png,.jpg"></s:file> <s:fielderror name="u.picture">error<s:param></s:param></s:fielderror> <s:submit action="uploadimage" style="60px;height:20px;" value="上传"/> </form> </body> </html>