求JAVA WEB项目文件夹上传下载方法

求JAVA WEB项目文件夹上传下载方法,第1张

两种实现方式,一种是借助FTP服务器实现上传下载,引入相应的jar包,直接拷贝网上现成的代码,另一种通过原生的代码,读取文件夹及里面的文件,通过io流处理,存放到指定地址,或数据库设计一个大字段,存放二进制流数据

你是在说网站搬家。

1、ftp工具将主机中的emlog下载content文件夹

2、emlog博客网站后台,左侧导航栏——数据——备份数据库——点击开始备份按钮,备份完成后,会自动通过浏览器下载

3、在新服务器上安装一个和原来版本一样的emlog,要保证数据库前缀,数据库名和原来的一样,不然可能出现问题。管理员账号也跟原来的一样,即使你使用了新的账号密码,恢复数据后,还得用的账号密码登陆博客系统后台进行 *** 作。

4、恢复网站数据

将原来系统中备份的数据库文件(emlog_为前缀的sql文件)通过emlog后台导入到新的emlog系统中,注意,一定要先恢复数据,然后再进行下一步的上传原来系统中导出的用户文件(content文件夹中的文件)。

5、恢复用户文件

将从原来的虚拟主机空间中导出的content文件夹使用ftp上传到新的虚拟主机空间中,覆盖新安装系统中的content文件夹,d出对话框,点击全部覆盖即可。

6、更新缓存:

登陆重新安装好的emlog后台(用原来备份系统的管理员账号密码,因为现在数据库已经恢复为原来的了),进入数据界面,点击“更新缓存”,搬家成功!

随着Internet技术的发展和跨平台需求的日益增加 Web Services的应用越来越广 我们不但需要通过Web Services传递字符串信息 而且需要传递二进制文件信息 下面 我们就分别介绍如何通过Web Services从服务器下载文件到客户端和从客户端通过Web Services上载文件到服务器

一 通过Web Services显示和下载文件

我们这里建立的Web Services的名称为GetBinaryFile 提供两个公共方法 分别是GetImage()和GetImageType() 前者返回二进制文件字节数组 后者返回文件类型 其中 GetImage()方法有一个参数 用来在客户端选择要显示或下载的文件名字 这里我们所显示和下载的文件可以不在虚拟目录下 采用这个方法的好处是 可以根据权限对文件进行显示和下载控制 从下面的方法我们可以看出 实际的文件位置并没有在虚拟目录下 因此可以更好地对文件进行权限控制 这在对安全性有比较高的情况下特别有用 这个功能在以前的ASP程序中可以用Stream对象实现 为了方便读者进行测试 这里列出了全部的源代码 并在源代码里进行介绍和注释

首先 建立GetBinaryFile a x文件

我们可以在VS NET里新建一个C#的aspxWebCS工程 然后 添加新项 选择 Web服务 并设定文件名为 GetBinaryFile a x 在 查看代码 中输入以下代码 即 GetBinaryFile a x cs

using System;

using System Collections;

using System ComponentModel;

using System Data;

using System Diagnostics;

using System Web;

using System Web UI;

using System Web Services;

using System IO;

namespace aspxWebCS

{

///

/// GetBinaryFile 的摘要说明

/// Web Services名称 GetBinaryFile

/// 功能 返回服务器上的一个文件对象的二进制字节数组

///

[WebService(Namespace=

Description= 在Web Services里利用 NET框架进行传递二进制文件 )]

public class GetBinaryFile : System Web Services WebService

{

#region Component Designer generated code

//Web 服务设计器所必需的

private IContainer ponents = null;

///

/// 清理所有正在使用的资源

///

protected override void Dispose( bool disposing )

{

if(disposing &&ponents != null)

{

ponents Dispose();

}

base Dispose(disposing);

}

#endregion

public class Images: System Web Services WebService

{

///

/// Web 服务提供的方法 返回给定文件的字节数组

///

[WebMethod(Description= Web 服务提供的方法 返回给定文件的字节数组 )]

public byte[] GetImage(string requestFileName)

{

///得到服务器端的一个

///如果你自己测试 注意修改下面的实际物理路径

if(requestFileName == null || requestFileName == )

return getBinaryFile( D:\Picture JPG );

else

return getBinaryFile( D:\ + requestFileName);

}

///

/// getBinaryFile 返回所给文件路径的字节数组

///

///

public byte[] getBinaryFile(string filename)

{

if(File Exists(filename))

{

try

{

///打开现有文件以进行读取

FileStream s = File OpenRead(filename);

return ConvertStreamToByteBuffer(s);

}

catch(Exception e)

{

return new byte[ ];

}

}

else

{

return new byte[ ];

}

}

///

/// ConvertStreamToByteBuffer 把给定的文件流转换为二进制字节数组

///

///

public byte[] ConvertStreamToByteBuffer(System IO Stream theStream)

{

int b ;

System IO MemoryStream tempStream = new System IO MemoryStream();

while((b =theStream ReadByte())!= )

{

tempStream WriteByte(((byte)b ));

}

return tempStream ToArray();

}

[WebMethod(Description= Web 服务提供的方法 返回给定文件类型 )]

public string GetImageType()

{

///这里只是测试 您可以根据实际的文件类型进行动态输出

return image/jpg ;

}

}

}

}

观看地址 进入讨论组讨论

一旦我们创建了上面的a x文件 进行编译后 我们就可以编写客户端的代码来进行调用这个Web Services了

我们先 添加Web引用 输入 下面 我们编写显示文件的中间文件 GetBinaryFileShow aspx 这里 我们只需要在后代码里编写代码即可 GetBinaryFileShow aspx cs文件内容如下

using System;

using System Collections;

using System ComponentModel;

using System Data;

using System Drawing;

using System Web;

using System Web SessionState;

using System Web UI;

using System Web UI WebControls;

using System Web UI HtmlControls;

using System Web Services;

namespace aspxWebCS

{

///

/// GetBinaryFileShow 的摘要说明

///

public class GetBinaryFileShow : System Web UI Page

{

private void Page_Load(object sender System EventArgs e)

{

// 在此处放置用户代码以初始化页面

///定义并初始化文件对象

aspxWebCS GetBinaryFile Images oImage;

oImage = new aspxWebCS GetBinaryFile Images();

///得到二进制文件字节数组

byte[] image = oImage GetImage( );

///转换为支持存储区为内存的流

System IO MemoryStream memStream = new System IO MemoryStream(image);

///定义并实例化Bitmap对象

Bitmap bm = new Bitmap(memStream);

///根据不同的条件进行输出或者下载

Response Clear();

///如果请求字符串指定下载 就下载该文件

///否则 就显示在浏览器中

if(Request QueryString[ Download ]== )

{

Response Buffer = true;

Response ContentType = application/octet stream ;

///这里下载输出的文件名字 ok jpg 为例子 你实际中可以根据情况动态决定

Response AddHeader( Content Disposition attachment;filename=ok jpg );

}

else

Response ContentType = oImage GetImageType();

Response BinaryWrite(image);

Response End();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN 该调用是 ASP NEeb 窗体设计器所必需的

//

InitializeComponent();

base OnInit(e);

}

///

/// 设计器支持所需的方法 不要使用代码编辑器修改

/// 此方法的内容

///

private void InitializeComponent()

{

this Load += new System EventHandler(this Page_Load);

}

#endregion

}

}

最后 我们就编写最终的浏览页面 GetBinaryFile aspx 这个文件很简单 只需要aspx文件即可 内容如下

<%@ Page language="c#" Codebehind="GetBinaryFileaspxcs" AutoEventWireup="false"

Inherits="aspxWebCSGetBinaryFile" %>Inherits= aspxWebCS GetBinaryFile %>

runat= server >runat= server >下载文件

收藏地址:进入讨论组讨论

using System;

using System Collections;

using System ComponentModel;

using System Data;

using System Diagnostics;

using System Web;

using System Web Services;

using System IO;

namespace aspxWebCS

{

///

/// Upload 的摘要说明

///

[WebService(Namespace=

Description= 在Web Services里利用 NET框架进上载文件 )]

public class Upload : System Web Services WebService

{

public Upload()

{

//CODEGEN 该调用是 ASP NEeb 服务设计器所必需的

InitializeComponent();

}

#region Component Designer generated code

//Web 服务设计器所必需的

private IContainer ponents = null;

///

/// 设计器支持所需的方法 不要使用代码编辑器修改

/// 此方法的内容

///

private void InitializeComponent()

{

}

///

/// 清理所有正在使用的资源

///

protected override void Dispose( bool disposing )

{

if(disposing &&ponents != null)

{

ponents Dispose();

}

base Dispose(disposing);

}

#endregion

[WebMethod(Description= Web 服务提供的方法 返回是否文件上载成功与否 )]

public string UploadFile(byte[] fs string FileName)

{

try

{

///定义并实例化一个内存流 以存放提交上来的字节数组

MemoryStream m = new MemoryStream(fs);

///定义实际文件对象 保存上载的文件

FileStream f = new FileStream(Server MapPath( ) + \

+ FileName FileMode Create);

///把内内存里的数据写入物理文件

m WriteTo(f);

m Close();

f Close();

f = null;

m = null;

return 文件已经上传成功 ;

}

catch(Exception ex)

{

return ex Message;

}

}

}

}

using System;

using System Collections;

using System ComponentModel;

using System Data;

using System Drawing;

using System Web;

using System Web SessionState;

using System Web UI;

using System Web UI WebControls;

using System Web UI HtmlControls;

using System Web Services;

using System IO;

namespace aspxWebCS

{

///

/// Upload 的摘要说明

/// 利用该方法通过Web Services上载文件

///

public class Upload : System Web UI Page

{

protected System Web UI HtmlControls HtmlInputFile MyFile;

protected System Web UI WebControls Button Button ;

private void Page_Load(object sender System EventArgs e)

{

// 在此处放置用户代码以初始化页面

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN 该调用是 ASP NEeb 窗体设计器所必需的

//

InitializeComponent();

base OnInit(e);

}

///

/// 设计器支持所需的方法 不要使用代码编辑器修改

/// 此方法的内容

///

private void InitializeComponent()

{

this Button Click += new System EventHandler(this Button _Click);

this Load += new System EventHandler(this Page_Load);

}

#endregion

private void Button _Click(object sender System EventArgs e)

{

///首先得到上载文件信息和文件流

if(MyFile PostedFile != null)

{

System Web >1、资源文件放在服务器下是完全没问题的,一个网站发布后也不会随便更新的。
2、如果资源文件过多、或都过大,是建议放到服务器下的,会占用服务器过大的空间,你可以在tomcat中再配置一个虚拟路径,指向一个盘符下一个文件夹(如:D:/images),在 tomcat 的 serverxml 中设置<Context docBase="D:/images" path="/img" />,然后你就可以用你的服务器地址+/img/+资源路径(注意:这个资源路径是相对D:/images的相对路径)去访问资源了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/12866518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存