struts服务器图片显示,Java Struts图片上传至指定文件夹并显示图片功能

继上一次利用Servlet实现图片上传,这次利用基于MVC的Struts框架,封装了Servlet并简化了JSP页面跳转。

JSP上传页面

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

并且必须是method="post"

pageEncoding="utf-8"%>

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

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

设置文件上传最大值

/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显示图片

pageEncoding="utf-8"%>

success

成功

%24%7BimgFileName%7D

总结

以上所述是小编给大家介绍的Java Struts图片上传至指定文件夹并显示图片功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值