动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   程序开发 → 阅读文章

 Java Servlet 编程及应用之八

作者来源: 
阅读 数 633 人次 , 2006-4-27 17:59:00 


  Java Servlet 在网络上的编程应用,如利用Servlet 上传和下载文件、Servlet 的数据库编程、在Servlet 中发送和接受邮件以及Java Servlet 在RMI和XML等方面的应用,由于篇幅有限,在这里就不在多介绍了,下面再举一个Servlet 上传的例子。

  在Web 应用程序中,用户向服务器上传文件是非常普遍的操作。使用Servlet 实现文件的上传是比较简单的。

  编程思路:下面的UploadServlet.java ,其主要功能为从InputStream 中读取文件内容,将上传文件保存在根目录下,且文件名与上传文件的文件名一致。

  UploadServlet.java 的源代码如下:(代码节选)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadServlet extends HttpServlet
{
//default maximum allowable file size is 1000k
static final int MAX_SIZE = 1024000;
//instance variables to store root and success message
String rootPath, successMessage;
/**
* init method is called when servlet is initialized.
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//get path in which to save file
rootPath = config.getInitParameter("RootPath");
if (rootPath == null)
{
rootPath = "/";
}
/*Get message to show when upload is complete. Used only if
a success redirect page is not supplied.*/
successMessage = config.getInitParameter("SuccessMessage");
if (successMessage == null)
{
successMessage = "File upload complete!";
}
}
/**
* doPost reads the uploaded data from the request and writes
* it to a file.
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
{
ServletOutputStream out=null;
DataInputStream in=null;
FileOutputStream fileOut=null;

try
{
/*set content type of response and get handle to output
stream in case we are unable to redirect client*/
response.setContentType("text/plain");
out = response.getOutputStream();

//get content type of client request
String contentType = request.getContentType();
out.println("\ncontentType= " + contentType);

//make sure content type is multipart/form-data
if(contentType != null && contentType.indexOf(
"multipart/form-data") != -1)
{
//open input stream from client to capture upload file
in = new DataInputStream(request.getInputStream());
//get length of content data
int formDataLength = request.getContentLength();
out.println("\nContentLength= " + formDataLength);

//allocate a byte array to store content data
byte dataBytes[] = new byte[formDataLength];
//read file into byte array
int bytesRead = 0;
int totalBytesRead = 0;
int sizeCheck = 0;
while (totalBytesRead < formDataLength)
{
//check for maximum file size violation
sizeCheck = totalBytesRead + in.available();
if (sizeCheck > MAX_SIZE)
{
out.println("Sorry, file is too large to upload.");
return;
}

...........
...........
  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:Hibernate的缓存机制介绍
· 下一篇:Java 理论与实践:哈希
· 基于Nokia S60的游戏开发之三
· .NET正则表达式使用高级技巧之组的概念
· 数据结构与算法(C#实现)系列---N叉树(一)
· 在Pocket PC上编写游戏之六
· 全面掌握Windows XP的压缩功能


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077