资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Java项目中利用Struts将图片上传到指定文件夹-创新互联

怎么在Java项目中利用Struts将图片上传到指定文件夹?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站设计、绥德网络推广、重庆小程序开发公司、绥德网络营销、绥德企业策划、绥德品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;成都创新互联为所有大学生创业者提供绥德建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

JSP上传页面

上传一定要为form加上enctype="multipart/form-data",表示提交的数据时二进制的

并且必须是method="post"

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






 
 



struts.xml配置(maven项目放在resources)

 
 
   
      设置文件上传大值
     
       
        /fail.jsp 
        /success.jsp
       
     
  

TestStruts.java控制类

一定要提供三个属性

File img;
String imgFileName;
String imgContentType;

然后为这3个属性提供getter setter方法

package com.controller;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestStruts extends ActionSupport{
  private File img;
  private String imgFileName;
  private String imgContentType;
  public File getImg(){
    return img;
  }
  public String getimgFileName(){
    return imgFileName;
  }
  public String getImgContentType(){
    return imgContentType;
  }
  public void setImg(File img){
    this.img = img;
  }
  public void setImgFileName(String imgFileName){
    this.imgFileName = imgFileName;
  }
  public void setImgFileContentType(String imgContentType){
    this.imgContentType = imgContentType;
  }
  @SuppressWarnings("unchecked")
  public String logintest() throws IOException{
    Map p = ActionContext.getContext().getSession();
    p.put("imgFileName", imgFileName);
    File f = new File("D://imagebystruts");
    if (!f.exists()) { 
      f.mkdir(); 
    }     
    FileUtils.copyFile(img, new File(f, imgFileName));
    return "success";
  }
}

Tomcat中server.xml文件配置虚拟路径


success.jsp显示图片

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %> 



success


成功

        

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


新闻名称:怎么在Java项目中利用Struts将图片上传到指定文件夹-创新互联
URL网址:http://cdkjz.cn/article/deisec.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220