热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

JavaWeb文件上传开发实例

这篇文章主要为大家详细介绍了JavaWeb文件上传开发实例,如何进行文件上传操作,感兴趣的小伙伴们可以参考一下

文件上传有SmartUpload、Apache的Commons fileupload.我们今天介绍Commons fileupload的用法。 

1.commons-fileupload-1.3.1.jar
下载地址:http://xiazai.jb51.net/201608/yuanma/javawebfileupload(jb51.net).rar

2.web.xml


 
 UploadServlet
 com.servlet.UploadServlet
 
 filePath
 F:\myeclipse\Workspaces\King\WebRoot\uploadfile\
 
 
 
 
 UploadServlet
 /servlet/UploadServlet
 
3.UploadFile.jsp

  
上传文件
上传文件1
上传文件2
上传文件说明1
上传文件说明2

3.servlet

package com.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import sun.swing.FilePane;

public class UploadServlet extends HttpServlet {

 private String filePath;


 /**
  * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.getWriter().println("请以POST方式上传文件"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file1 = null, file2 = null; String description1 = null, description2 = null; response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); DiskFileUpload diskFileUpload = new DiskFileUpload();//解析request try { List fileList = diskFileUpload.parseRequest(request);//将结果放置到List中 out.println("遍历所有的FileItem...
"); this.log("遍历所有的FileItem..."); for(FileItem fileItem : fileList){ if(fileItem.isFormField()){//如果是文本域 if("description1".equals(fileItem.getFieldName())){ out.println("遍历到description1...
"); description1 = new String(fileItem.getString().getBytes(),"UTF-8"); } if("description2".equals(fileItem.getFieldName())){ out.println("遍历到description2...
"); description2 = new String(fileItem.getString().getBytes(),"UTF-8"); } }else{//否则为文件域 if("file1".equals(fileItem.getFieldName())){ File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); out.println("遍历到file1...
"); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"
"); //服务器端文件,放在uploadfile文件夹下 file1 = new File(filePath,remoteFile.getName()); file1.getParentFile().mkdirs();//创建文件夹路径 file1.createNewFile();//创建新文件 InputStream ins = fileItem.getInputStream();//FileItem的内容 OutputStream ous = new FileOutputStream(file1);//输出到文件中 try{ byte[] buffer = new byte[1024];//字节缓存 int len = 0;//实际缓存长度 while((len=ins.read(buffer)) > -1){//循环读入缓存 ous.write(buffer,0,len); } out.println("已保存文件"+file1.getAbsolutePath()+"
"); } finally{ ous.close(); ins.close(); } } if("file2".equals(fileItem.getFieldName())){ File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); out.println("遍历到file2...
"); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"
"); //服务器端文件,放在uploadfile文件夹下 file2 = new File(filePath,remoteFile.getName()); file2.getParentFile().mkdirs();//创建文件夹路径 file2.createNewFile();//创建新文件 InputStream ins = fileItem.getInputStream();//FileItem的内容 OutputStream ous = new FileOutputStream(file2);//输出到文件中 try{ byte[] buffer = new byte[1024];//字节缓存 int len = 0;//实际缓存长度 while((len=ins.read(buffer)) > -1){//循环读入缓存 ous.write(buffer,0,len); } out.println("已保存文件"+file2.getAbsolutePath()+"
"); } finally{ ous.close(); ins.close(); } } } } out.println("Request 解析完毕"); } catch (FileUploadException e) { // TODO 自动生成的 catch 块 out.println(""); out.println(""); out.println(" "); out.println(" "); if(file1 != null){ out.println("
"); out.println("
file1;
"); out.println("
"+file1.getName()+""); out.println("
"); out.println("
"); } if(file2 != null){ out.println("
"); out.println("
file2;
"); out.println("
"+file2.getName()+""); out.println("
"); out.println("
"); } out.println("
"); out.println("
description1:
"); out.println("
"); out.println(description1); out.println("
"); out.println("
"); out.println("
"); out.println("
description2:
"); out.println("
"); out.println(description2); out.println("
"); out.println("
"); out.println(" "); out.println(""); out.flush(); out.close(); } } public String getServletInfo() { return "This is my default servlet created by Eclipse"; } public void init() throws ServletException { // Put your code here filePath = getServletConfig().getInitParameter("filePath"); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
可爱的你公馆_698
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有