java文件读取和文件创建
java文件读取和文件创建
文章目录
- java文件读取和文件创建
- 前言
- 一、任意文件创建
- 二、任意文件读取
前言
java文件读取和文件创建,在jsp中的应用
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>webvul</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
一、任意文件创建
<%@ page import="java.io.FileOutputStream" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>fileCreate</title>
</head>
<body>
<%
FileOutputStream fos = new FileOutputStream("C:\\Users\\fly\\IdeaProjects\\webvul\\web\\"+request.getParameter("name"));
fos.write(request.getParameter("c").getBytes());
fos.flush();
fos.close();
%>
</body>
</html>
成功创建test.txt文件
执行成功
二、任意文件读取
<%@ page import="java.io.FileInputStream" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>fileRead</title>
</head>
<body>
<%
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\fly\\IdeaProjects\\webvul\\web\\"+request.getParameter("file"));
int tmp;
while ((tmp=fileInputStream.read())!=-1){
out.write(tmp);
}
%>
</body>
</html>
还没有评论,来说两句吧...