欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java以struts2为例介绍如何实现图片上传

程序员文章站 2024-03-07 21:33:21
总的说图片上传有两种方式,一种是把图片文件写到数据库中,另一种是存到服务器文件目录中。写到数据库中的图片文件需要转换成二进制流的格式,占用数据库空间比较,适合少量图片的存储...

总的说图片上传有两种方式,一种是把图片文件写到数据库中,另一种是存到服务器文件目录中。写到数据库中的图片文件需要转换成二进制流的格式,占用数据库空间比较,适合少量图片的存储,比如说,系统中某些小图标,写到数据库中的优点是比较安全,不容易被用户不小心删除。

在struts2中实现(以图片上传为例)

1.fileupload.jsp代码清单如下:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>the fileuplaoddemo in struts2</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:submit label="提交"></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:property value ="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 = new file(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 configuration 2.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-ref name="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>

以上内容是小编给大家介绍的java struts2中如何实现图片上传的全部内容,希望大家喜欢。