Java上传文件
来源:互联网 发布:淘宝图片怎么加护盾 编辑:程序博客网 时间:2024/06/15 09:35
1.首先创建一个Javaweb项目(idea)
2.建一个简单的jsp页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/12/5 Time: 11:36 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body><form id="form" name="form" method="post" enctype="multipart/form-data" action="uploadServlet.do" > 上传的文件:<input id="file" name="file" type="file"/> <button type="submit" name="btn" id="btn">提交</button></br>${result}</form></body></html>
注意:form表单中需要添加一个属性和它的值 enctype="multipart/form-data",input属性的type设为file
3.创建一个servlet
package com.zj.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;@WebServlet(name = "uploadServlet")public class uploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("开始上传文件"); //获取前台传过来的文件信息 InputStream filestram=request.getInputStream(); //设置读取文件的编码格式 InputStreamReader inputStreamReader=new InputStreamReader(filestram,"utf-8"); //将传过来的文件信息保存在临时文件中去 String tempFile="E:/tempfile"; File file =new File(tempFile); //将上传信息写入到临时文件中去 FileOutputStream fileOutputStream=new FileOutputStream(file); //设置写入文件的编码格式 OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8"); //循环读取上传上来的文件内容 ,并写入临时文件 byte[] b=new byte[1024]; int n; while ((n=filestram.read(b))!=-1){ fileOutputStream.write(b,0,n); } //关闭输入输出流 fileOutputStream.close(); filestram.close(); //获取上传文件的名称 RandomAccessFile randomAccessFile=new RandomAccessFile(tempFile,"r"); randomAccessFile.readLine(); String str=randomAccessFile.readLine(); int beginIndex=str.lastIndexOf("=")+2;//文件不要放在桌面上 System.out.println("beginIndex="+beginIndex); int endIndex=str.lastIndexOf("\""); System.out.println("endIndex="+endIndex); String filename= str.substring(beginIndex,endIndex); //解决获取文件名称乱码 String f=new String(filename.getBytes("ISO-8859-1"),"utf-8"); System.out.println("文件名="+f); //获取上传文件的内容,并保存在某一路径下 //重新指定文件的指针位置 randomAccessFile.seek(0); //定义读取文件内容的开始位置 long startposition=0; int i=1; //获取读取文件的开始位置 while ((n=randomAccessFile.readByte())!=-1&&i<=4){ if (n=='\n'){ startposition=randomAccessFile.getFilePointer(); i++; } } System.out.println("文件读取的开始位置="+startposition); startposition=randomAccessFile.getFilePointer()-1; //获取读取文件的结束位置 //重新指定文件的指针位置 randomAccessFile.seek(randomAccessFile.length());//将指针指向临时问件的结尾 //定义文件的结束位置 long endposition=randomAccessFile.getFilePointer(); int j=1; while (endposition>=0&&j<=6){ endposition--; randomAccessFile.seek(endposition); if (randomAccessFile.readByte()=='\n'){ j++; } } endposition=endposition-1; System.out.println("endposition="+endposition); //定义写入文件的路径 String Path="E:/tempfiles"; //创建写入文件夹 File pathfile=new File(Path); //判断创建的文件夹是否存在 if (!pathfile.exists()){ pathfile.mkdir(); } //创建保存文件(pathfile:文件的路径,f:需要读取的临时文件) File savefile=new File(pathfile,f); //设置对文件访问的方式为读写 RandomAccessFile r=new RandomAccessFile(savefile,"rw"); //从临时文件中读取文件内容(根据起止位置获取) randomAccessFile.seek(startposition); //循环写入读取文件内容 while (startposition<endposition){ r.write(randomAccessFile.readByte()); startposition=randomAccessFile.getFilePointer(); } //关闭输入输出流 r.close(); randomAccessFile.close(); file.delete();//返回信息给页面request.setAttribute("result","上传成功!!");RequestDispatcher requestDispatcher=request.getRequestDispatcher("/01.jsp");requestDispatcher.forward(request, response);} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }}
4.在web.xml中配置servlet
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"><servlet> <servlet-name>uploadServlet</servlet-name> <servlet-class>com.zj.servlet.uploadServlet</servlet-class></servlet> <servlet-mapping> <servlet-name>uploadServlet</servlet-name> <url-pattern>/uploadServlet.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>01.jsp</welcome-file> </welcome-file-list></web-app>
阅读全文
0 0
- Java 文件上传 JQ 文件上传
- java文件上传原理
- java文件上传原理
- java上传文件
- 上传文件 JSP+java
- JAVA上传文件
- java上传文件
- java fileupload上传文件
- java-文件上传案例
- JAVA上传EXCEL文件
- java上传文件
- java 超大文件上传
- java上传文件实例
- java 文件上传
- JAVA 上传文件类
- JAVA之"文件上传"
- java文件上传
- java文件上传方法
- JAVA 基本类型数组的初始化(原理篇)
- 编译dubbo项目方法
- vue-导出后端返回数据到excel中
- AliOS Things v1.1.1新特性
- 阿里云虚拟化平台携手麦腾举办AI时代的加速核心武器交流会
- Java上传文件
- mysql5.7解压安装
- JQuery页面加载初始化常用方法
- 在java中怎样将Date类型转换成字符串类型
- session 与 application
- 如何写一个播放器-解析MNVideoPlayer(三)
- 每天一个linux命令(7):mv命令
- (PTA详解)L1-003. 个位数统计
- 别忽悠我,征信真的有黑名单吗?