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

<?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;
	}
	
	@Override
	public 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类的核心代码。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值