archieve: homework6 change: java version
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="com.codeverse.userSettings.MarscodeWorkspaceAppSettingsState">
|
||||||
|
<option name="ckgOperationStatus" value="SUCCESS" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+10
@@ -1,4 +1,14 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
+31
-4
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -18,6 +18,31 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-fileupload</groupId>
|
||||||
|
<artifactId>commons-fileupload</artifactId>
|
||||||
|
<version>1.3.3</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.7</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||||
|
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
|
||||||
|
<version>1.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.taglibs</groupId>
|
||||||
|
<artifactId>taglibs-standard-impl</artifactId>
|
||||||
|
<version>1.2.5</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
@@ -35,7 +60,8 @@
|
|||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency> </dependencies>
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
@@ -43,6 +69,7 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
<version>3.3.2</version>
|
<version>3.3.2</version>
|
||||||
</plugin> </plugins>
|
</plugin>
|
||||||
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.msksbr.homework6.fileupload;
|
||||||
|
|
||||||
|
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.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
@WebServlet(name = "DownloadServlet", urlPatterns = "/DownloadServlet")
|
||||||
|
public class DownloadServlet extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse
|
||||||
|
response) throws ServletException, IOException {
|
||||||
|
//设置ContentType字段值
|
||||||
|
response.setContentType("text/html;charset=utf-8");
|
||||||
|
//获取所要下载的文件名称
|
||||||
|
String filename = request.getParameter("filename");
|
||||||
|
//下载文件所在目录
|
||||||
|
String folder = "/upload/";
|
||||||
|
// 通知浏览器以下载的方式打开
|
||||||
|
response.addHeader("Content-Type", "application/octet-stream");
|
||||||
|
response.addHeader("Content-Disposition",
|
||||||
|
"attachment;filename=" + filename);
|
||||||
|
folder = folder + filename;
|
||||||
|
// 通过文件流读取文件
|
||||||
|
InputStream in = getServletContext().getResourceAsStream(folder);
|
||||||
|
// 获取response对象的输出流
|
||||||
|
OutputStream out = response.getOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int len;
|
||||||
|
//循环取出流中的数据
|
||||||
|
while ((len = in.read(buffer)) != -1) {
|
||||||
|
out.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doPost(HttpServletRequest request, HttpServletResponse
|
||||||
|
response) throws ServletException, IOException {
|
||||||
|
doGet(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package com.msksbr.homework6.fileupload;
|
||||||
|
|
||||||
|
import org.apache.commons.fileupload.FileItem;
|
||||||
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||||
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.MultipartConfig;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@WebServlet(name = "UploadServlet", value = "/UploadServlet")
|
||||||
|
@MultipartConfig
|
||||||
|
public class UploadServlet extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public void doGet(HttpServletRequest request,
|
||||||
|
HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
try {
|
||||||
|
//设置ContentType字段值
|
||||||
|
response.setContentType("text/html;charset=utf-8");
|
||||||
|
// 创建DiskFileItemFactory工厂对象
|
||||||
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||||
|
//设置文件缓存目录,如果该目录不存在则新创建一个
|
||||||
|
File f = new File("uploadTemp");
|
||||||
|
if (!f.exists()) {
|
||||||
|
f.mkdirs();
|
||||||
|
}
|
||||||
|
// 设置文件的缓存路径
|
||||||
|
factory.setRepository(f);
|
||||||
|
// 创建 ServletFileUpload对象
|
||||||
|
ServletFileUpload fileupload = new ServletFileUpload(factory);
|
||||||
|
//设置字符编码
|
||||||
|
fileupload.setHeaderEncoding("utf-8");
|
||||||
|
// 解析 request,得到上传文件的FileItem对象
|
||||||
|
List<FileItem> fileitems = fileupload.parseRequest(request);
|
||||||
|
//获取字符流
|
||||||
|
PrintWriter writer = response.getWriter();
|
||||||
|
// 遍历集合
|
||||||
|
for (FileItem fileitem : fileitems) {
|
||||||
|
// 判断是否为普通字段
|
||||||
|
if (fileitem.isFormField()) {
|
||||||
|
// 获得字段名和字段值
|
||||||
|
String name = fileitem.getFieldName();
|
||||||
|
if (name.equals("name")) {
|
||||||
|
//如果文件不为空,将其保存在value中
|
||||||
|
if (!fileitem.getString().equals("")) {
|
||||||
|
String value = fileitem.getString("utf-8");
|
||||||
|
writer.print("上传者:" + value + "<br />");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 获取上传的文件名
|
||||||
|
String filename = fileitem.getName();
|
||||||
|
//处理上传文件
|
||||||
|
if (filename != null && !filename.equals("")) {
|
||||||
|
writer.print("上传的文件名称是:" + filename + "<br />");
|
||||||
|
// 截取出文件名
|
||||||
|
filename = filename.substring(filename.lastIndexOf("\\") + 1);
|
||||||
|
// 文件名需要唯一
|
||||||
|
filename = UUID.randomUUID() + "_" + filename;
|
||||||
|
// 在服务器创建同名文件
|
||||||
|
String webPath = "/upload/";
|
||||||
|
//将服务器中文件夹路径与文件名组合成完整的服务器端路径
|
||||||
|
String filepath = getServletContext()
|
||||||
|
.getRealPath(webPath + filename);
|
||||||
|
// 创建文件
|
||||||
|
File file = new File(filepath);
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
file.createNewFile();
|
||||||
|
// 获得上传文件流
|
||||||
|
InputStream in = fileitem.getInputStream();
|
||||||
|
// 使用FileOutputStream打开服务器端的上传文件
|
||||||
|
FileOutputStream out = new FileOutputStream(file);
|
||||||
|
// 流的拷贝
|
||||||
|
byte[] buffer = new byte[1024];//每次读取1个字节
|
||||||
|
int len;
|
||||||
|
//开始读取上传文件的字节,并将其输出到服务端的上传文件输出流中
|
||||||
|
while ((len = in.read(buffer)) > 0)
|
||||||
|
out.write(buffer, 0, len);
|
||||||
|
// 关闭流
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
// 删除临时文件
|
||||||
|
fileitem.delete();
|
||||||
|
writer.print("上传文件成功!<br />");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doPost(HttpServletRequest request,
|
||||||
|
HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
doGet(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>文件下载</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<a href=
|
||||||
|
"${pageContext.request.contextPath}/DownloadServlet?filename=avatar.png">
|
||||||
|
文件下载</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="${pageContext.request.contextPath}/UploadServlet" method="post" enctype="multipart/form-data">
|
||||||
|
<table width="600px">
|
||||||
|
<tr>
|
||||||
|
<td>上传者</td>
|
||||||
|
<td><input type="text" name="name"></td>
|
||||||
|
<tr>
|
||||||
|
<td>上传文件</td>
|
||||||
|
<td><input type="file" name="myfile"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2"><input type="submit" value="提交"></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user