esoe
1 year ago
20 changed files with 306 additions and 84 deletions
@ -0,0 +1,37 @@ |
|||||||
|
package ru.molokoin.home.servlets; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
|
||||||
|
import jakarta.servlet.ServletException; |
||||||
|
import jakarta.servlet.annotation.WebServlet; |
||||||
|
import jakarta.servlet.http.HttpServlet; |
||||||
|
import jakarta.servlet.http.HttpServletRequest; |
||||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
/** |
||||||
|
* Сервлет удаляющий файл из хранилища |
||||||
|
*/ |
||||||
|
@WebServlet(name = "Delete", value = "/delete") |
||||||
|
public class Delete extends HttpServlet{ |
||||||
|
private String STORAGE_PATH = "/srv/apps/home/exchange"; |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||||||
|
request.setCharacterEncoding("UTF-8"); |
||||||
|
response.setContentType("text/html; charset=UTF-8"); |
||||||
|
|
||||||
|
//Узнать имя файла из запроса
|
||||||
|
String fileName = request.getParameter("filename"); |
||||||
|
|
||||||
|
//Удаление файла
|
||||||
|
File fileToDelete = new File(STORAGE_PATH + File.separator + fileName); |
||||||
|
fileToDelete.delete(); |
||||||
|
//после скачивания возвращаем пользователя в хранилище
|
||||||
|
response.sendRedirect("storage"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
package ru.molokoin.home.servlets; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
|
||||||
|
import jakarta.servlet.ServletException; |
||||||
|
import jakarta.servlet.annotation.WebServlet; |
||||||
|
import jakarta.servlet.http.HttpServlet; |
||||||
|
import jakarta.servlet.http.HttpServletRequest; |
||||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||||
|
import ru.molokoin.home.util.HardDrive; |
||||||
|
|
||||||
|
@WebServlet(name = "Download", value = "/download") |
||||||
|
public class Download extends HttpServlet{ |
||||||
|
private String STORAGE_PATH = HardDrive.root; |
||||||
|
private final int ARBITARY_SIZE = 1048;//размер буфера при чтении файла
|
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||||||
|
request.setCharacterEncoding("UTF-8"); |
||||||
|
//response.setContentType("text/html; charset=UTF-8");
|
||||||
|
|
||||||
|
response.setContentType("text/plain; charset=UTF-8"); |
||||||
|
//Узнать имя файла из запроса
|
||||||
|
String fileName = request.getParameter("filename"); |
||||||
|
response.setHeader("Content-disposition", "attachment; filename=" + fileName); |
||||||
|
|
||||||
|
//Отдаем файл с сервера
|
||||||
|
File file = new File(STORAGE_PATH + File.separator + fileName); |
||||||
|
try(InputStream in = new FileInputStream(file); |
||||||
|
OutputStream out = response.getOutputStream()) { |
||||||
|
|
||||||
|
byte[] buffer = new byte[ARBITARY_SIZE]; |
||||||
|
|
||||||
|
int numBytesRead; |
||||||
|
while ((numBytesRead = in.read(buffer)) > 0) { |
||||||
|
out.write(buffer, 0, numBytesRead); |
||||||
|
} |
||||||
|
} |
||||||
|
//после скачивания возвращаем пользователя в хранилище
|
||||||
|
response.sendRedirect("storage"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,76 @@ |
|||||||
|
package ru.molokoin.home.util; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.nio.file.Files; |
||||||
|
import java.nio.file.Path; |
||||||
|
import java.nio.file.Paths; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
import java.util.stream.Stream; |
||||||
|
|
||||||
|
/** |
||||||
|
* Методы работы с данными на жестком диске |
||||||
|
*/ |
||||||
|
public class HardDrive { |
||||||
|
public static String root = "/srv/apps/home/exchange"; |
||||||
|
|
||||||
|
public static String getRoot() { |
||||||
|
return root; |
||||||
|
} |
||||||
|
public static void setRoot(String root) { |
||||||
|
HardDrive.root = root; |
||||||
|
} |
||||||
|
/** |
||||||
|
* Метод, возвращающий список файлов в корне хранилища приложения |
||||||
|
* @return |
||||||
|
* @throws IOException |
||||||
|
*/ |
||||||
|
private static Set<String> listFiles(String path) throws IOException{ |
||||||
|
Stream<Path> stream = Files.list(Paths.get(path)); |
||||||
|
Set<String> list = stream |
||||||
|
.filter(file -> !Files.isDirectory(file)) |
||||||
|
.map(Path::getFileName) |
||||||
|
.map(Path::toString) |
||||||
|
.collect(Collectors.toSet()); |
||||||
|
stream.close(); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
private static HashMap<String, Boolean> getMap(String path) throws IOException{ |
||||||
|
Stream<Path> stream = Files.list(Paths.get(path)); |
||||||
|
HashMap<String, Boolean> map = new HashMap<>(); |
||||||
|
return map; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static void showFiles(File[] files) { |
||||||
|
for (File file : files) { |
||||||
|
if (file.isDirectory()) { |
||||||
|
System.out.println("Directory: " + file.getAbsolutePath()); |
||||||
|
showFiles(file.listFiles()); // Calls same method again.
|
||||||
|
} else { |
||||||
|
System.out.println("File: " + file.getAbsolutePath()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* тестируем работу методов HardDrive |
||||||
|
* @param args |
||||||
|
*/ |
||||||
|
public static void main(String[] args) { |
||||||
|
File dir = new File("C:\\Users\\Strannik\\Documents\\esoe\\code\\home\\src\\main\\webapp\\content"); |
||||||
|
new File(dir + File.separator + "inner").mkdirs(); |
||||||
|
//showFiles(dir.listFiles());
|
||||||
|
System.out.println(Arrays.toString(dir.listFiles())); |
||||||
|
for (File file : dir.listFiles()) { |
||||||
|
System.out.println(file.getName()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,3 +1,6 @@ |
|||||||
ru\molokoin\home\servlets\Storage.class |
ru\molokoin\home\servlets\Storage.class |
||||||
ru\molokoin\home\entities\Media.class |
ru\molokoin\home\entities\Media.class |
||||||
|
ru\molokoin\home\servlets\Delete.class |
||||||
ru\molokoin\home\servlets\Main.class |
ru\molokoin\home\servlets\Main.class |
||||||
|
ru\molokoin\home\util\HardDrive.class |
||||||
|
ru\molokoin\home\servlets\Download.class |
||||||
|
@ -1,3 +1,6 @@ |
|||||||
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Storage.java |
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Storage.java |
||||||
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\entities\Media.java |
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\entities\Media.java |
||||||
|
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Download.java |
||||||
|
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Delete.java |
||||||
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Main.java |
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\servlets\Main.java |
||||||
|
C:\Users\Strannik\Documents\esoe\code\home\src\main\java\ru\molokoin\home\util\HardDrive.java |
||||||
|
Loading…
Reference in new issue