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

网友投稿 224 2023-07-03


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

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

1、页面index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

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

%>


姓名:


上传文件:


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

xmlns:xsi="http://w3.org/2001/XMLSchema-instanchttp://e"

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

http:///upload/*

index.jsp

xmlns:xsi="http://w3.org/2001/XMLSchema-instanchttp://e"

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

http:///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.httphttp://.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。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java多线程实现聊天客户端和服务器
下一篇:bootstrap读书笔记之CSS组件(上)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~