这里有新鲜出炉的Java并发编程示例,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要为大家详细介绍了java组件smartupload实现上传文件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
1、页面index.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>index.jsp</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- </head>
- <body>
- <form action="upload" method="post" enctype="multipart/form-data">
- <br>
- 姓名:<input type="text" name="uname"/>
- <br>
- 上传文件:<input type="file" name="pic"/>
- <br>
- <input type="submit" value="提交"></input>
- </form>
- </body>
- </html>
2、action跳转到了upload的servlet,所以要web.xml里面配置,web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- 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">
- <servlet>
- <servlet-name>uploadAction</servlet-name>
- <servlet-class>com.pop.action.SmartuploadAction</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>uploadAction</servlet-name>
- <url-pattern>/upload/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
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。
来源: http://www.phperz.com/article/17/1114/359713.html