struts 图片上传

导入包

struts2-core-2.0.11.2.jar

xwork-2.0.5.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

commons-logging-1.0.4.jar

commons-io-1.4.jar

commons-fileupload-1.2.1.jar

Action

package com.web.pb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 图片上传
* @author wei
*
*/
public class Upload extends ActionSupport {

private static final long serialVersionUID = 6634487347910754252L;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;
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 getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}


public String upload() throws Exception
{
//得到上传文件的后缀名
String b=getUploadContentType();
String houzhui=b.substring(b.indexOf("/")+1, b.length());
houzhui=(houzhui.equals("pjpeg")?"jpg":houzhui);

//得到文件的新名字

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String mingzi= sdf.format(new Date())+"." + houzhui;

// 创建文件
FileOutputStream fos = new FileOutputStream(getSavePath() + "//" + mingzi);
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;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}

Src下 struts.xml

<?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="utf-8"/>
<package name="upload1" extends="struts-default">
<action name="upload" class="com.web.pb.Upload" method="upload" >
<interceptor-ref name="fileUpload">
<param name ="allowedTypes">
image/bmp,image/PNG,image/gif,image/JPEG,image/jpg,image/pjpeg
</param>
<param name="maximumSize">
102400000
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/photo</param>
<result name="success" >success.jsp</result>
<result name="input"> /index.jsp</result>

</action>
</package>
</struts>

测试页面

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<s:fielderror></s:fielderror>
<s:form method="post" action="upload" enctype="multipart/form-data">
<s:textfield name="title" label="title"></s:textfield>
<s:file name="upload" label="file"></s:file>
<s:submit></s:submit>
</s:form>
<body>

</body>
</html>

测试报错

创建保存图片文件夹 photo

你可能感兴趣的:(struts)