`
左手边
  • 浏览: 94355 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2上传图片时,图片后缀名的取得方式

 
阅读更多

1、直接上例子,前台jsp如下,这个例子是多文件上传

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'index.jsp' starting page</title>
	</head>

	<body>
		<center>
			<font color='red'><s:fielderror></s:fielderror></font>
			<form action="${pageContext.request.contextPath}/upload_up.action"
				method="post" enctype="multipart/form-data">
				<input type="file" name="img" /> <br/>
				<input type="file" name="img" /> <br/>
				测试普通参数:<input type="text" name="userid"> <br/>
				<input type="submit" value="上传"/>
			</form>
				
		</center>
	</body>
</html>

2、这是action代码

public class UploadAction extends ActionSupport {

	private File[] img;
	private String userid;
	private String[] imgFileName;
	private String[] imgContentType;

	public File[] getImg() {
		return img;
	}

	public void setImg(File[] img) {
		this.img = img;
	}

	public String[] getImgFileName() {
		return imgFileName;
	}

	public void setImgFileName(String[] imgFileName) {
		this.imgFileName = imgFileName;
	}

	public String[] getImgContentType() {
		return imgContentType;
	}

	public void setImgContentType(String[] imgContentType) {
		this.imgContentType = imgContentType;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String up() {
		for (int i = 0; i < img.length; i++) {
			InputStream is = null;
			OutputStream os = null;
			try {
				is = new FileInputStream(img[i]);
				String realPath = ServletActionContext.getServletContext()
						.getRealPath("/upload")
						+ "/";
				String fileName = new IPTimeStamp()
						.getIPTimeStamp(ServletActionContext.getRequest()
								.getRemoteAddr());
				fileName += imgFileName[i].substring(imgFileName[i].lastIndexOf("."));
				os = new FileOutputStream(new File(realPath + fileName));
				// 边读边写
				byte[] data = new byte[1024];
				int length = 0;
				while ((length = is.read(data)) != -1) {
					os.write(data, 0, length);
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (os != null) {
					try {
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println(imgContentType[i]);
			}
		}
		return null;
	}

}

 多文件上传这里为file数组private File[] img; 而且需要添加这两个属性,private String[] img FileName;
    private String[] img ContentType;注意属性的名字为xxxFileName和xxxContentType形式,这样就可以通过

imgFileName[i].substring(imgFileName[i].lastIndexOf("."))形式得到后缀名了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics