smartupload java_java组件smartupload实现上传文件功能

使用jsp和serlvet来实现最简单的上传,供大家参考,具体内容如下

1、页面index.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

index.jsp

姓名:

上传文件:

2、action跳转到了upload的servlet,所以要web.xml里面配置,web.xml:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

uploadAction

com.pop.action.SmartuploadAction

uploadAction

/upload/*

index.jsp

3、映射到的action文件,SmartuploadAction.java:

package com.pop.action;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.jsp.JspFactory;

import javax.servlet.jsp.PageContext;

import com.soft4j.httpupload4j.Request;

import com.soft4j.httpupload4j.SmartUpload;

import com.soft4j.httpupload4j.SmartUploadException;

public class SmartuploadAction extends HttpServlet {

private static final long serialVersionUID = -8610555375032925108L;

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

resp.setCharacterEncoding("utf-8");

SmartUpload su = new SmartUpload();

// 由于multipart/form-data的传输原因导致req不能使用,所以使用smartupload产生的request

Request reqest = su.getRequest();

// 获得pageContext对象

PageContext pageContext = JspFactory.getDefaultFactory()

.getPageContext(this, req, resp, null, true, 8192, true);

su.initialize(pageContext);

try {

su.upload();

// 上传到本项目的upload目录

su.save("upload");

} catch (SmartUploadException e) {

e.printStackTrace();

}

// 使用smartupload产生的reqest对象来获得页面传递的参数

String uname = reqest.getParameter("uname");

System.out.println(uname);

}

}

最后说明:使用的组件包为smartupload.zip。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值