您的位置:首页 > 编程语言 > Java开发

struts2实现图片上传功能

2017-02-17 17:02 267 查看
第一步jar包:maven pom.xml中的坐标如下

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->

  <dependency>

   <groupId>commons-io</groupId>

   <artifactId>commons-io</artifactId>

   <version>2.2</version>

  </dependency>

文件上传的需要的jar

<dependency>

   <groupId>commons-fileupload</groupId>

   <artifactId>commons-fileupload</artifactId>

   <version>1.3.1</version>

  </dependency>

第二步:先写前端页面如

web-inf下的**.jsp

<script type="text/javascript">

 $(function(){

  $('#image').on('click',imageUpload);

 });

 function imageUpload(){

  $("[name='image']").click();

  upload();

 }

 function fileUpload(){

  

 }

 function upload(){

  $('form').submit();

 }

</script>

</head>

<body>

  <h1>欢迎来到主页...这里实现的功能将是上传照片</h1>

  <div>

   <form action="upload.action" method="post" enctype="multipart/form-data" >

    <input type="hidden" value="${imageFileName}" id="fileName">

    <input type="file" name="image" style="display:none;" onclick="fileUpload();"/>

   </form>

   <div style="padding-top: 10px;float: right;">

     <img id="image" name="" src="${pageContext.request.contextPath}/image/${imageFileName}" width="100px" height="100px"/>

   </div>

</body>
第三步配置struts2.xml
<package name="myPackage" extends="struts-default">
<action name="upload" class="ssh.zzx.action.FileUploadAction" >

   <interceptor-ref name="fileUpload">

    <!-- 限制文件的类型 -->

    <param name="allowedTypes">

     image/bmp,image/png,image/gif,image/jpeg,image/jpg

    </param>

    <!-- 限制文件的大小 -->

    <param name="maximumSize">10M</param>

   </interceptor-ref>

   <interceptor-ref name="defaultStack"></interceptor-ref>

   <result>/WEB-INF/jsp/index.jsp</result>

   <result name="input">/error.jsp</result>

  </action>
<package>
接下来就是ssh.zzx.action.FileUploadAction 这个Action类了

package ssh.zzx.action;
import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;
import org.apache.struts2.ServletActionContext;

import org.apache.struts2.convention.annotation.Namespace;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")

public class FileUploadAction extends ActionSupport {
 /**

  *

  */

 private static final long serialVersionUID = 1L;
 /**

  *

  */
 private File image;// 得到上传的文件
 private String imageFileName;// 得到上传文件的名称
 private String imageContentType;// 得到上传文件的类型
 private Map<String,Object> session;
 public String execute() throws IOException {
  if(image==null){
   return "input";
}

  

  session=ActionContext.getContext().getSession();

  // 根据上下文获得根目录

  String path = ServletActionContext.getServletContext().getRealPath(

    "/image");

  // 用文件输入流读取文件

  InputStream is = new FileInputStream(image);

  // 判断文件存不存在

  File file = new File(path, imageFileName);

  if (!file.exists()) {

   file.createNewFile();

  }

  // 用文件输出流创建一个文件,这里创建的文件名为imageFileName

  OutputStream os = new FileOutputStream(file);

  // 按字节输出

  byte[] buffer = new byte[500];

  int length = 0;

  while (-1 != (length = is.read(buffer, 0, buffer.length))) {

   os.write(buffer);

  }

  session.put("imageFileName", imageFileName);

  os.close();

  is.close();

  return SUCCESS;

 }
 public File getImage() {

  return image;

 }
 public void setImage(File image) {

  this.image = image;

 }
 public String getImageFileName() {

  return imageFileName;

 }
 public void setImageFileName(String imageFileName) {

  this.imageFileName = imageFileName;

 }
 public String getImageContentType() {

  return imageContentType;

 }
 public void setImageContentType(String imageContentType) {

  this.imageContentType = imageContentType;

 }
 public Map<String, Object> getSession() {

  return session;

 }
 public void setSession(Map<String, Object> session) {

  this.session = session;

 }

}

到这里就ok了!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: