struts2实现图片上传功能

第一步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;" οnclick="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了!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值