`
tomp110
  • 浏览: 31795 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

common-net包的ftp上传进度

    博客分类:
  • Java
阅读更多
//上传文件
public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception{
    MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest)request;
    MultipartFile file = fileRequest.getFile("file");
    String name = new String(file.getOriginalFilename().getBytes(), "ISO-8859-1");
    transSize = 0;
    totalSize = file.getSize();
    fileName = file.getOriginalFilename();
    PrintWriter out = response.getWriter();
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
    int buff = 1024;
    ftpClient.setBufferSize(buff);
    int contentLen = 0;
    byte[] upload = new byte[1024];
    OutputStream output = ftpClient.appendFileStream(ftpClient.printWorkingDirectory() + "/" + name);
    InputStream input = file.getInputStream();
    try{
        do{
            contentLen = input.read(upload);
            output.write(upload, 0 ,contentLen);
	   transSize += contentLen;
        }while(contentLen >= 0);
    } catch(ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
    } finally {
        input.close();
        output.flush();
        out.write("true");
        out.close();
    }
}

//取得进度		
public void getPercent(HttpServletRequest request, HttpServletResponse response) throws Exception{
    PrintWriter out = response.getWriter();
    out.write((double)transSize/(double)totalSize + "");
    out.close();
}


上传用到了spring的MVC,可以忽略。
分享到:
评论

相关推荐

    common-io,common-net打包奉送

    Apache Commons Net库(common-net)主要处理各种网络协议,包括FTP、Telnet、NNTP等。FTP部分提供了丰富的API,使得开发者可以轻松地执行FTP相关的任务。以下是一些关键功能: 1. FTP连接管理:创建和管理FTP连接...

    SWT(JFace) FTP客户端实现

    7. `commons-net-ftp-2.0.jar` - Apache Commons Net库,提供FTP协议的实现。 接下来,我们分析给出的代码片段。`FTPWindow.java` 文件中,开发者使用JFace和SWT创建了一个应用程序窗口,展示了如何与FTP服务器进行...

    FileZilla两个安装包

    在开发环境中,特别是在使用`common-net`库进行文件上传时,FileZilla可以作为一个测试工具,帮助开发者验证文件上传的正确性和速度。`common-net`是一个Java库,提供了网络I/O功能,包括FTP和HTTP协议的支持。...

    服务器架设和管理

    - 在Linux系统中安装CUPS(Common Unix Printing System)。 - **配置打印机服务器**: - 添加打印机:包括指定打印机型号、连接方式等。 - 设置打印队列:用于管理打印作业。 - 分享打印机:使网络中的其他用户...

    libcurl的C#版本LibCurlNet

    4. **上传和下载**:提供便捷的文件上传和下载功能,包括断点续传、进度回调,以及自定义数据流处理。 5. **回调机制**:通过注册回调函数,可以实时监控网络请求的进度,如数据接收、发送、错误处理等。 6. **...

Global site tag (gtag.js) - Google Analytics