SSH框架实现图片上传

第一步,先写上传的jsp页面upload.jsp,注意:一定要在form里面加enctype="multipart/form-data"

<!--在开头加上这个,以防万一-->
<%@taglib prefix="s" uri="/struts-tags"%>


<form name="frm" method="post" action="driver-license" enctype="multipart/form-data">
       <input type="file" name="frontPath1"/>  
       <button type="submit" class="panel-button btn btn-info">上传</button>
 </form>

第二步,写对应的driver-licenseAction类,注意:定义realPath1地址时你必须注意,如果是在本机上执行,未在服务器(这里说的服务器是腾讯云这种买的,不是Tomcat)上执行,地址写本机硬盘的绝对路径,千万不能写在当前项目目录下,否则你根本无法把上传的图片保存到你指定的目录

public class DriverLicenseAction extends ActionSupport{
    //frontPath1ContentType是固定格式,是由前一步enctype="multipart/form-data"调用的
    private String frontPath1ContentType;
    /*frontPath1FileName是固定格式,是由前一步enctype="multipart/form-data"调用的,用于获            
取上传的文件名,你也可以对frontPath1FileName进行赋值,修改保存后的图片名字*/
	private String frontPath1FileName;
	private File frontPath1;
    
    //省略get和set方法,这里使用的是注解的方式
    @Action(value = "driver-license", results = { @Result(name = "licenseUpdate", location = "hello.jsp")})
	public String LicenseActionUpdate() throws IOException {
        /*这一步你必须注意,如果是在本机上执行,未在服务器上执行,地址写本机硬盘的绝对路
径,千万不能写在当前项目目录下,否则你根本无法把上传的图片保存到你指定的目录,这里的地址可
以根据个人修改*/
        String realPath1 = "D:\\eclipse-workspace\\image\\front";
        File file1 = new File(realPath1, frontPath1FileName);
        FileUtils.copyFile(frontPath1, file1);
        return "licenseUpdate";
    }
}

第三步,写显示的hello.jsp页面

<!--头部加入这个-->
<%@taglib prefix="s" uri="/struts-tags"%>


<img src="<s:property value="realPath1 + frontPath1FileName"/>"/><br/>

嗯,基本就完成了,其实我在网上找了许多的教程,但是很少有提及上面说的两点注意的,希望我的帖子对你有帮助

欢迎留言交流!!!

 

猜你喜欢

转载自blog.csdn.net/fbvukn/article/details/85316221