打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java实现图片上传
    在struts2中实现(以图片上传为例)
1.FileUpload.jsp代码清单如下:
<%@ page language="java" import="java.util.*"pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>The FileUplaodDemo InStruts2</title>
</head>

<body>
<s:form action="fileUpload" method="post"enctype="multipart/form-data" namespace="/">
<s:file name="myFile" label="MyFile"></s:file>
<s:textfield name="caption"label="Caption"></s:textfield>
<s:submitlabel="提交"></s:submit>
</s:form>
</body>
</html>

2.ShowUpload.jsp的功能清单如下:
<%@ page language="java" import="java.util.*"pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>ShowUpload</title>
</head>

<body>
<div style ="padding: 3px; border: solid 1px#cccccc; text-align: center" >
<img src ="UploadImages/<s:propertyvalue ="imageFileName"/> "/>
<br />
<s:property value ="caption"/>
</div >
</body>
</html>

3.FileUploadAction.java的代码清单如下 :

package com.chris;

import java.io.*;
import java.util.Date;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024 ;

//注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
//所以同时要提供myFileContentType,myFileFileName的set方法

private File myFile; //上传文件
private String contentType;//上传文件类型
private String fileName; //上传文件名
private String imageFileName;
private String caption;//文件说明,与页面属性绑定

public void setMyFileContentType(String contentType) {
System.out.println("文件类型 : " + contentType);
this .contentType = contentType;
}

public void setMyFileFileName(String fileName) {
System.out.println("文件名称 : " + fileName);
this .fileName = fileName;
}

public void setMyFile(File myFile) {
this .myFile = myFile;
}

public String getImageFileName() {
return imageFileName;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
this .caption = caption;
}

private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src),BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst),BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

@Override
public String execute() {
imageFileName = new Date().getTime() +getExtention(fileName);
File imageFile = newFile(ServletActionContext.getServletContext().getRealPath("UploadImages") + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}

注:此时仅为方便实现Action所以继承ActionSupport,并Overrider execute()方法
在struts2中任何一个POJO都可以作为Action

4.struts.xml清单如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/"extends="struts-default">
<action name="fileUpload"class="com.chris.FileUploadAction">
<interceptor-refname="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>

5.web.xml清单如下:
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter >
<filter-name > struts-cleanup</filter-name >
 <filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class >
 </filter >
 <filter-mapping>
<filter-name > struts-cleanup</filter-name >
 <url-pattern > /*</url-pattern >
 </filter-mapping>

 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
<filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>Index.jsp</welcome-file>
 </welcome-file-list>

</web-app>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Struts 2中实现文件上传
JS中的JSON对象
struts2多文件动态下载及中文解决方案(此文测试过,没错)
JSP上传图片并生成缩略图
CKEditor图片上传实现详细步骤(使用Struts 2)
简单的Java Filter过滤用法实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服