Struts2框架下实现向服务器上传图片

来源:互联网 发布:eplan 软件 编辑:程序博客网 时间:2024/05/16 09:49
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>        <constant name="struts.i18n.encoding" value="GBK" /><package name="default" extends="struts-default"><action name="onload" class="com.jht.OnAction">                        <!--配置文件上传拦截器,限制上传格式为图片,文件最大为2M-->                        <interceptor-ref name="fileUpload"><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param><param name="maximumSize">2000000</param></interceptor-ref><interceptor-ref name="defaultStack" />                                                <!--文件上传后将保存在/upload路径下,upload。jsp是上传页面,onloadpro.jsp是上传成功后的显示页面-->                        <param name="savePath">/upload</param><result name="success">/onloadpro.jsp</result><result name="input">/upload.jsp</result></action></package></struts>
以上为struts.xml文件的核心配置。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html><html>  <head>    <title>11</title>  </head>    <body>  <span style="color:red"><s:fielderror/></span><form method="post" action="onload.action"enctype="multipart/form-data">文件标题:<input type="text" name="title" /><br>选择文件:<input type="file" name="upload" /><br><input type="submit" value="提交" /></form>  </body></html>
以上为upload.jsp页面的核心代码。

<%@ page language="java" contentType="text/html;charset=GBK"%><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>上传成功</title></head><body> <center> 上传成功!<br> 文件标题:<s:property value=" + title" /><br> 文件为:<br> <img src="<s:property value="'upload/'+uploadFileName"/>"/><br> </center></body></html>
以上为onloadpro.jsp页面的核心代码。

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class OnAction extends ActionSupport{private String title;private File upload;private String uploadContentType;private String uploadFileName;private String savePath;private String getSavePath() throws Exception {return ServletActionContext.getRequest().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}@Overridepublic String execute() throws Exception {FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());FileInputStream fis = new FileInputStream(getUpload());byte[] buffer = new byte[1024];int len = 0;while ( (len = fis.read(buffer)) > 0 ) {fos.write(buffer, 0 ,len);}return SUCCESS;}}
以上为Action类的核心代码。




0 0