文件上传

本文介绍如何使用jspSmartUpload组件实现文件上传功能。通过一个简单的Servlet示例展示了如何处理用户上传的文件,并将其保存到指定路径。文章包含了完整的代码实现。

<%@ page language="java" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>file upload</title>

</head>

<body>
<font size="5" color="#FF0000">
<b>文件上传 — 使用jspsmart upload组件</b>
</font><br>
<form name="selectfile" enctype="multipart/form-data" method="post" action="servlet/servletUpload">
<p>文件名称:
<input type="file" name="ulfile" size="20" maxlength="80">
</p>
<p>存储路径:
<input type="text" name="PATH" size="30" maxlength="50">
</p>
<p>
<input type="submit" value="上传">
<input type="reset" value="清除">
</p>
</form>
</body>
</html>



package com.bjsxt;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;

public class servletUpload extends HttpServlet {

private static final long serialVersionUID = 1L;

private ServletConfig config;
/**
* 初始化Servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
/**
* 处理GET请求
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR><BR>");
out.println("The method of the HTML form must be POST.");
out.println("</BODY>");
out.println("</HTML>");
}
/**
* 响应POST请求
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR>");

// 变量定义
int count=0;
SmartUpload mySmartUpload = new SmartUpload();

String uploadPath = mySmartUpload.getRequest().getParameter("PATH");

if(uploadPath == null || "".equals(uploadPath)) {
uploadPath = "C:\\apache-tomcat-6.0.16\\webapps\\ServletTest\\upload";
}

try {
// 初始化
mySmartUpload.initialize(config,request,response);
// 上载
mySmartUpload.upload();
// 保存上载文件到指定目录
// PATH为form表单提交过来的
count = mySmartUpload.save(uploadPath);
// 显示处理结果
out.println(count + " file uploaded.");
} catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println("</BODY>");
out.println("</HTML>");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值