根据需要写一个上传文件的小程序,比较简单就用serlvet写。首先是要上传文件到服务器,然后是解压删除zip包。同时写一个监听器,用于进度条展现上传进度。
首先是页面Upload.jsp:前台需要上传一个文件和上传文件名称(对应服务器的相应目录),Ajax提交请求以实现进度条变化。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
然后是文件上传的serlvet:UploadServletMy.java request请求包含了文件类型的input和select下拉菜单,遍历判断顺序应该和jsp页面顺序对应,保证pathR的值
不为空。
package com.helloweenvsfei.servlet;
}
接着是解压zip的serlvet:FileUnzipServlet.java
package com.helloweenvsfei.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.doczip.ZipDecompressing;
public class FileUnzipServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.getWriter().println("请以 POST 方式上传文件");
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
File file1 = (File) session.getAttribute("file");
String pathR = (String) session.getAttribute("pathR");
//上传后解压zip文件
if(file1.getAbsolutePath().contains(".zip")){
File zipFile = new File(file1.getAbsolutePath());
String pathZip = "D:/temp/aaa/";
File pathFile = new File(pathZip);
File temp = null;
File[] filelist = pathFile.listFiles();
String zipFileName = zipFile.getName();
String prefix=zipFileName.substring(zipFileName.lastIndexOf("."));
String checkname =zipFileName.substring(0, zipFileName.length()-prefix.length());
System.out.println("名字名字"+checkname);
out.println("");
out.println("");
out.println("
package com.helloweenvsfei.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 文件上传进度状态类
* @author
*
*/
public class fileUploadStatusServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
Object status = session.getAttribute("key");//获取上传进度百分比
if(status==null) return;
PrintWriter out = response.getWriter();
out.write(status.toString());
out.flush();
out.close();
}
}
package com.helloweenvsfei.servlet;
import java.text.NumberFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
public class UploadProgressListener implements ProgressListener {
private HttpSession session;
private long kiloBytes=-1;
public UploadProgressListener(HttpServletRequest request) {
this.session = request.getSession();
}
//pBytes
public void update(long PBytesRead, long PContentLength, int PItems) {
Long KBytes=PBytesRead/1024;
if(kiloBytes==KBytes)
{
return;
}
kiloBytes=KBytes;
System.out.println("正在读取项目"+KBytes);
if(PContentLength==-1)
{
System.out.println("目前已经读取了"+PBytesRead+"字节数据");
}
else
{
System.out.println("目前已经读取了"+PContentLength+"中的"+PBytesRead+"字节数据");
}
//获取上传进度的百分比
double read=((double)KBytes)/(PContentLength/1024);
NumberFormat nf=NumberFormat.getPercentInstance();
session.setAttribute("key", nf.format(read));
System.out.println("进度时间:"+nf.format(read));
}
}