发布于 2015-04-15 07:59:13 | 183 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Struts Java MVC框架

Struts 是Apache软件基金会(ASF)赞助的一个开源项目。它最初是Jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目。它通过采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架,是MVC经典设计模式中的一个经典产品。


struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 
<constant name="struts.action.extension" value="do" /> -->

<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />

<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />

<!-- 其实在url里我们可以这样访问 http://localhost:8080/exam/login!checkLogin.action ;
login!checkLogin login是我的struts.properties配置文件中的一个action的名字 而checkLogin是这个action类里的一个方法。 
我们可以使用action名 + 感叹号 + 方法名进行方法调用 这个在开发阶段开启可以加速我们开发人员的开发调试,
 但是在项目完成时还是设为false的好。毕竟直接访问类的方法是不安全的。 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />

<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />

<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="20971520" />

<!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir 
<constant name="struts.multipart.saveDir" value="d:/tmp" /> -->

<package name="default" namespace="/" extends="struts-default">
<!-- 固定文件下载 -->
<action name="downLoadAction" class="com.yc.web.actions.DownLoadAction">
<!-- 指定下载文件的路径,该路径相对于web应用程序所在的目录 -->
<param name="inputPath">/WEB-INF/uploadFiles/2014100901.zip</param>

<result name="success" type="stream">
<!-- 指定下载文件的内容类型 -->
<param name="contentType">application/zip</param>
<!-- inputName默认值是inputStream, 如果action中用于读取下载文件内容的属性名是inputStream, 那么可以省略这个参数 -->
<param name="inputName">inputStream</param>

<!-- 指定被下载文件名 -->
<param name="contentDisposition">filename="2014100901.zip"</param>

<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">2048</param>
</result>
</action>

<!-- 指定文件下载 -->
<action name="downLoadAction1" class="com.yc.web.actions.DownLoadAction1">
<!--type 为 stream 应用 StreamResult 处理 -->
<result name="success" type="stream">
<!-- 下载文件类型定义 二进制文件 -->
<param name="contentType">application/octet-stream</param>

<!-- 指定被下载文件的入口输入流. inputName默认值是inputStream, 如果action中用于读取下载文件内容的属性名是inputStream, 
那么可以省略这个参数 它将会指示 StreamResult 通过 inputName 属性值的 getter 方法, 比如这里就是 getInputStream() 
来获取下载文件的内容,意味着你的 Action 要有这个方法 -->

<param name="inputName">inputStream</param>

<!-- 指定被下载文件名 attachment: 附件;附录;附着;依恋 默认为 inline(在线打开),设置为 attachment 
将会告诉浏览器下载该文件,filename 指定下载文 件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 
作为文件名, 这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名 -->
<param name="contentDisposition">attachment;fileName="${fileName}"</param>

<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">2048</param>
</result>
</action>

<action name="uploadAction" class="com.yc.web.actions.SingleUpLoadAction">
<!-- 动态设置savePath的属性值 -->
<param name="savePath">/uploadFile</param>
<result name="success">/WEB-INF/pages/message.jsp</result>

<interceptor-ref name="fileUpload">
<!-- 文件过滤 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>

<!-- 文件大小, 以字节为单位 -->
<param name="maximumSize">20971520</param>
</interceptor-ref>

<!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
<interceptor-ref name="defaultStack" />
</action>

<action name="uploadAction1" class="com.yc.web.actions.MoreUpLoadAction">
<!-- 动态设置savePath的属性值 -->
<param name="savePath">/uploadFile</param>

<result name="success">/WEB-INF/pages/message.jsp</result>

<interceptor-ref name="fileUpload">
<!-- 文件过滤 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>

<!-- 文件大小, 以字节为单位 -->
<param name="maximumSize">20971520</param>
</interceptor-ref>

<!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
<interceptor-ref name="defaultStack" />
</action>

</package>
</struts>

 

 DownLoadAction.java 固定文件下载

 public class DownLoadAction extends ActionSupport {
private String inputPath; // 下载文件的路径,在struts.xml配置.

public String getInputPath() {
return inputPath;
}

public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}

public InputStream getInputStream() {
//如果你要下载任意目录下的文件(而不仅限于Web应用程序目录),
//那么只需要修改action中返回文件输入流的方式即可,例如:return new FileInputStream(inputPath).
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}

@SuppressWarnings("static-access")
public String execute() {
return super.SUCCESS;
}
}

DownLoadAction1.java  指定文件下载

 public class DownLoadAction1 extends ActionSupport {
private String fileName; //下载文件路劲

//对于配置中的 ${fileName}, 获得下载保存时的文件名  
public String getFileName() throws UnsupportedEncodingException {
 return new String(fileName.getBytes(), "ISO8859-1");
}

public void setFileName(String fileName) throws UnsupportedEncodingException{
//用UTF-8重新编码文件名,解决中文乱码
this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
}

 //获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容  
public InputStream getInputStream() throws UnsupportedEncodingException{
//如果fileName是相对路径
//如果你要下载任意目录下的文件(而不仅限于Web应用程序目录),
//那么只需要修改action中返回文件输入流的方式即可,例如:return new FileInputStream(inputPath).
return ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/uploadFiles/"+fileName);

//如果fileName是绝对路径
// return new BufferedInputStream(new FileInputStream(fileName));

}

@SuppressWarnings("static-access")
public String execute() {
 //这里可加入权限控制    
return super.SUCCESS;
}
}

 
SingleUpLoadAction.java   单文件上传
 
public class SingleUpLoadAction extends ActionSupport {
// 封装上传文件域的属性
private File image;

// 封装上传文件类型的属性
private String imageContentType;

// 封装上传文件名的属性
private String imageFileName;

// 接受依赖注入的属性
private String savePath;

@Override
public String execute() {
FileOutputStream fos = null;
FileInputStream fis = null;

try {
// 建立文件输出流
fos = new FileOutputStream(getSavePath()+"\"+getImageFileName());
// 建立文件上传流
fis = new FileInputStream(getImage());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
System.out.println("文件上传失败");
e.printStackTrace();
} finally {
close(fos, fis);
}
return SUCCESS;
}

/**
 * 返回上传文件的保存位置
 * 
 * @return
 */
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath); 
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public File getImage() {
return image;
}

public void setImage(File image) {
this.image = image;
}

public String getImageContentType() {
return imageContentType;
}

public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

private void close(FileOutputStream fos, FileInputStream fis) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("FileInputStream关闭失败");
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("FileOutputStream关闭失败");
e.printStackTrace();
}
}
}
}

MoreUpLoadAction.java   多文件上传
 
public class MoreUpLoadAction extends ActionSupport {
private List<File> file;
private List<String> fileFileName;  
private List<String> fileContentType;
// 接受依赖注入的属性
private String savePath;

public List<File> getFile() {  
return file;  
}  

public void setFile(List<File> file) {  
this.file = file;  
}  

public List<String> getFileFileName() {  
return fileFileName;  
}  

public void setFileFileName(List<String> fileFileName) {  
this.fileFileName = fileFileName;  
} 

public List<String> getFileContentType() {  
return fileContentType;  
}  

public void setFileContentType(List<String> fileContentType) {  
this.fileContentType = fileContentType;  
}  

/**
 * 返回上传文件的保存位置
 * 
 * @return
 */
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath); 
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

@Override  
public String execute() throws Exception {  
//获取文件存储路径  
String path =ServletActionContext.getServletContext().getRealPath(savePath);

for(int i=0; i<file.size();i++){  
OutputStream os = new FileOutputStream(new File(path,fileFileName.get(i)));  
InputStream is = new FileInputStream(file.get(i));  

byte[] buf = new byte[1024];  
int length = 0 ;  

while(-1 != (length = is.read(buf) )){  
os.write(buf, 0, length) ;  
}  
is.close();  
os.close();  
}  
return SUCCESS;  
}  
}

html 内容

 <body>
         <a href="downLoadAction">我要下载</a> <br/><br/>
         
         <a href="downLoadAction1?fileName=2014100901.zip" title="2014100901.zip">下载</a><br/><br/>
         
         <a href="downLoadAction1?fileName=飞机订票系统.zip" title="飞机订票系统.zip">下载</a><br/><br/>
         
          <form action="uploadAction" enctype="multipart/form-data" method="post">
             文件:<input type="file" name="image">
                <input type="submit" value="上传" />
        </form>
        <br /><br />
        
<form action="uploadAction1" enctype="multipart/form-data" method="post" >  
上传文件:<input type="file" name="file"><br/>  
上传文件: <input type="file" name="file"><br/> <!-- 两个名字相同 都是file1 -->  
<input type="submit" value="提交"/>  
     </form>  
  </body>


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务