jsp多文件上传到数据库Word文件下载.docx

上传人:b****3 文档编号:16661505 上传时间:2022-11-25 格式:DOCX 页数:13 大小:47.20KB
下载 相关 举报
jsp多文件上传到数据库Word文件下载.docx_第1页
第1页 / 共13页
jsp多文件上传到数据库Word文件下载.docx_第2页
第2页 / 共13页
jsp多文件上传到数据库Word文件下载.docx_第3页
第3页 / 共13页
jsp多文件上传到数据库Word文件下载.docx_第4页
第4页 / 共13页
jsp多文件上传到数据库Word文件下载.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

jsp多文件上传到数据库Word文件下载.docx

《jsp多文件上传到数据库Word文件下载.docx》由会员分享,可在线阅读,更多相关《jsp多文件上传到数据库Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。

jsp多文件上传到数据库Word文件下载.docx

title>

FileUpload<

/title>

linkhref="

%=request.getContextPath()%>

/css/fileupload.css"

rel="

stylesheet"

type="

text/css"

/head>

bodybgcolor="

#cccccc"

divstyle="

padding-top:

30px;

padding-left:

30px"

fieldset

style="

width:

600px;

border-color:

#000000;

10px"

legend>

fontsize="

-1"

color="

#000000"

b>

上传图片<

/b>

/font>

/legend>

divclass="

ErrorDiv"

s:

fielderror/>

actionerror/>

/div>

formaction="

fileUpload.action"

method="

post"

theme="

simple"

enctype="

multipart/form-data"

tableborder="

1"

width="

500px"

tr>

tdclass="

tableTDCenter"

图片路径:

/td>

filename="

file"

cssClass="

fileInput"

/>

/tr>

colspan="

2"

submitvalue="

提交"

button"

/s:

submit>

/table>

form>

/fieldset>

/body>

/html>

很简单,就是个上传框和按钮,别的没有了。

至于一些css就是稍微的装饰装饰,一会也在下面写上!

上传成功的画面:

ImageList<

body>

iteratorvalue="

#list"

id="

picture"

divfloat"

imgsrc='

outPicture.action?

id=<

propertyvalue="

#picture.id"

'

iterator>

下面我们看看struts2的配置文件:

?

xmlversion="

1.0"

encoding="

DOCTYPEstrutsPUBLIC

"

-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

http:

//struts.apache.org/dtds/struts-2.0.dtd"

struts>

constantname="

struts.custom.i18n.resources"

value="

messageResource"

/>

struts.i18n.encoding"

packagename="

default"

extends="

struts-default"

--上传-->

actionname="

fileUpload"

class="

org.csdn.action.FileUploadAction"

interceptor-refname="

defaultStack"

paramname="

allowedTypes"

image/bmp,image/png,image/gif,image/jpg

/param>

paramname="

maximumSize"

404800<

/interceptor-ref>

resultname="

success"

/uploadSuccess.jsp<

/result>

input"

/fileUpload.jsp<

/action>

--输出显示-->

outPicture"

org.csdn.action.OutPictureAction"

/package>

/struts>

至于里面的东西是什么,大家肯定根据名字一看就知道了。

上面我引用了messageResource资源文件,为了显示错误消息。

比如上传类型不正确,超过大小,以及上传失败。

下面就是资源文件中的内容。

struts.messages.error.content.type.not.allowed=Thefileyouuploadedisnotaimage

struts.messages.error.file.too.large=thispicutreistoolarge

fileupload.fail=fileuploadisfail

如果没有重新定义上面两个key的话,显示的会是struts2默认的错误消息,很是难看,也乱起八糟的。

所以我重新定义了。

接着我们就开始写上传和输出图片的action,以及相关的类信息

上传的action:

importjava.io.File;

importjava.util.List;

importorg.csdn.service.FileUploadService;

importorg.csdn.vo.Picture;

importcom.opensymphony.xwork2.ActionContext;

importcom.opensymphony.xwork2.ActionSupport;

/**

*@authorclosewubq

*/

publicclassFileUploadActionextendsActionSupport{

privatestaticfinallongserialVersionUID=1L;

privateFilefile;

publicFilegetFile(){

returnfile;

}

publicvoidsetFile(Filefile){

this.file=file;

/**

*上传文件

@Override

publicStringexecute(){

FileUploadServicefuservice=newFileUploadService();

if(fuservice.fileUpload(file)){

List<

Picture>

list=fuservice.findAll();

ActionContextcxt=ActionContext.getContext();

cxt.put("

list"

list);

returnSUCCESS;

}else{

super.addActionError(this.getText("

fileupload.fail"

));

returnINPUT;

}

非常简单,因为我只单纯的用了struts2,没有用spring。

所以FileUploadService就硬编码写在里面了。

图片上传的主要业务类

importjava.io.FileInputStream;

importjava.io.InputStream;

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.util.ArrayList;

importorg.csdn.connection.GetConnection;

publicclassFileUploadService{

*上传图片到数据库

*@paramflie

*@returnboolean

*上传是否成功

publicbooleanfileUpload(Fileflie){

FileInputStreamin=null;

Connectionconn=null;

PreparedStatementps=null;

try{

in=newFileInputStream(flie);

Stringsql="

insertintosavepicture(picture)value(?

)"

;

conn=GetConnection.getConn();

if(conn==null){

System.out.println("

连接为null"

);

returnfalse;

ps=conn.prepareStatement(sql);

ps.setBinaryStream(1,in,in.available());

if(ps.executeUpdate()>

0){

GetConnection.close(conn,ps,null);

returntrue;

}else{

}catch(Exceptione){

System.out.println(e.getMessage());

*检索所有图片

*@returnlist

*返回所有图片记录

publicList<

findAll(){

list=newArrayList<

();

Picturepic=null;

ResultSetrs=null;

selectidfromsavepicture"

returnnull;

rs=ps.executeQuery();

while(rs.next()){

pic=newPicture();

pic.setId(rs.getInt("

id"

list.add(pic);

GetConnection.close(conn,ps,rs);

returnlist;

e.printStackTrace();

*根据图片ID获取流对象

*@paramid

*@returnInputStream

publicInputStreamgetPicById(intid){

InputStreamis=null;

selectpicturefromsavepicturewhereid=?

"

ps.setInt(1,id);

if(rs.next()){

is=rs.getBinaryStream("

returnis;

}catch(Exceptionex){

ex.printStackTrace();

简单的写了个jdbc链接的获取类。

importjava.sql.DriverManager;

importjava.sql.SQLException;

*获取数据库链接

publicclassGetConnection{

*获取数据库连接

*@returnConnection

*返回数据库连接

publicstaticConnectiongetConn(){

Class.forName("

com.mysql.jdbc.Driver"

).newInstance();

Stringurl="

jdbc:

mysql:

//localhost/csdn?

user=root&

password="

Connectionconnection=DriverManager.getConnection(url);

returnconnection;

*关闭连接释放资源

*@paramconn

*@paramrs

*@paramst

publicstaticvoidclose(Connectionconn,PreparedStatementps,ResultSetrs){

if(rs!

=null)

rs.close();

}catch(SQLExceptione){

if(ps!

ps.close();

if(conn!

conn.close();

还有保存图片信息的VO

publicclassPicture{

privateintid;

publicintgetId(){

returnid;

publicvoidsetId(intid){

this.id=id;

输出图片信息的Action

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.apache.struts2.ServletActionContext;

*图片输出

*

publicclassOutPictureActionextendsActionSupport{

publicStringexecute()throwsException{

HttpServletRequestrequest=ServletActionContext.getRequest();

intid=Integer.parseInt(request.getParameter("

FileUploadServiceservice=newFileUploadService();

InputStreamin=service.getPicById(id);

HttpServletResponseresponse=ServletActionContext.getResponse();

response.setContentType("

image/gif"

intsize=in.available();

byte[]image=newbyte[size];

in.read(image);

ServletOutputStreamout=response.getOutputStream();

out.write(image);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 金融投资

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1