首页 > 代码库 > JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

JSP+Servlet中使用cos.jar进行图片上传

 

upload.jsp

    <form action="FileServlet" method="post" enctype="multipart/form-data">        <input type="file" name="myfile">         <input type="text" name="cmt" value="example示例">         <input type="submit">    </form>

  enctype:HTML属性,规定在发送到服务器之前应该如何对表单数据进行编码。“发送前编码”

download.jsp

<a href="FileServlet?filename=1.xlsx">download</a>

Servlet:FileServlet.java

import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.File;import com.jspsmart.upload.Request;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;public class FileServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        String strName = request.getParameter("filename");        if (strName == null) {            return;        }        // new一下        SmartUpload su = new SmartUpload();        // 初始化        su.initialize(getServletConfig(), request, response);        // 初始化        su.setContentDisposition(null);        try {            su.downloadFile("up/" + strName);        } catch (SmartUploadException e) {            e.printStackTrace();        }        // 下载就不用重定向了,响应已经提交过了,会出错:        // Cannot call sendRedirect() after the response has been committed        //response.sendRedirect("upload.jsp");    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // new一下        SmartUpload su = new SmartUpload();        // 初始化        su.initialize(getServletConfig(), request, response);        try {            su.upload();            File f = su.getFiles().getFile(0);            // SAVE_PHYSICAL 操作系统根目录            // SAVE_VIRTUAL Web应用根目录            // SAVE_AUTO 组件自己选择            f.saveAs("up/" + f.getFileName(), SmartUpload.SAVE_VIRTUAL);        } catch (SmartUploadException e) {            e.printStackTrace();        }        // ↓↓↓取参数        String sevletP = request.getParameter("cmt");        System.out.println("直接获取参数不可以:" + sevletP);        Request request2 = su.getRequest();        String suP = request2.getParameter("cmt");        System.out.println("通过SmartUpload对象间接获取参数:" + suP);        // ↑↑↑取参数        // 转去下载页面        response.sendRedirect("download.jsp");    }}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">    <servlet>        <servlet-name>FileServlet</servlet-name>        <servlet-class>FileServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>FileServlet</servlet-name>        <url-pattern>/FileServlet</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>upload.jsp</welcome-file>    </welcome-file-list></web-app>

乱码问题:这个组件默认使用的编码是gb2312的,提交文件,JSP使用GBK可解决文件名乱码。

文件夹先建好,会在生成项目中生成这个文件夹

技术分享

 

JSP+Servlet中使用jspsmartupload.jar进行图片上传下载