jsp文件上传与下载

[size=large][color=blue][b]jsp中的文件上传例子[/b][/color][/size]
package org.svse.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class FileUpServlet extends HttpServlet {

private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
int ncount = 0;//记录上传文件数
// 声明一个上传组件对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(config, request, response);

//设置上传文件的最大3mb
su.setTotalMaxFileSize(3*1024*1024);

// 上传文件到服务器
try {
su.upload();
String uploadfile = request.getRealPath("upload");//得到文件存放的绝对路径
File upfile = new File(uploadfile);
if(!upfile.exists()){
upfile.mkdir();//如果不存在就要创建它
}
ncount = su.save("/upload");
out.println(ncount + "file(s) uploaded");
} catch (SmartUploadException e) {
e.printStackTrace();
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

public void init(ServletConfig config) throws ServletException {
this.config = config;
}

}

[size=large][color=blue][b]jsp文件下载[/b][/color][/size]
package org.svse.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class FileDownServlet extends HttpServlet {


private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

SmartUpload su = new SmartUpload();//实例化组件
su.initialize(config, request, response);//初始化
try {
su.downloadFile("upload/new.txt");//这个文件必须在你工程的upload目录中存在(现

在还是固定的)
} catch (SmartUploadException e) {

e.printStackTrace();
}

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}


public void init(ServletConfig config) throws ServletException {
this.config = config;
}

}

[size=large][color=blue][b]jsp界面
[/b][/color][/size]
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>


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


<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="FileUpServlet" method="post" enctype="multipart/form-data" >
请选择你要上传的文件:<input type="file" name="file1" /><br />
<input type="submit" value=" 上传 ">
</form>

<a href="FileDownServlet">下载文件</a>

</body>
</html>


注意:在使用smart工具上传文件时,表单的提交方式method="post" ,编码enctype="multipart/form-data"。在下面的附件中提供了完整的例子和smart工具的jar包(支持中文文件名的上传)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值