提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
以下问题:chatpgt都可以进行解决!!!!
zip文件的上传与下载(压缩与解压)
下载:
import java.io.*;
import java.util.zip.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadZipServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 指定要压缩的文件夹路径
String sourceFolder = "C:\\path\\to\\your\\source\\folder";
// 设置响应内容类型为ZIP文件
response.setContentType("application/zip");
// 指定ZIP文件名,并使用UTF-8编码
String zipFileName = "下载的文件.zip";
String encodedFileName = new String(zipFileName.getBytes("UTF-8"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");
try {
// 创建一个输出流,将ZIP数据写入响应流
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
// 调用递归方法将文件夹中的文件添加到ZIP文件中
addToZipFile(sourceFolder, sourceFolder, zos);
// 关闭ZIP输出流
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void addToZipFile(String folderPath, String sourceFolder, ZipOutputStream zos)
throws IOException {
File folder = new File(folderPath);
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
// 如果是文件夹,递归调用addToZipFile
addToZipFile(file.getPath(), sourceFolder, zos);
} else {
// 如果是文件,将文件添加到ZIP中
String entryName = file.getPath().substring(sourceFolder.length() + 1);
ZipEntry zipEntry = new ZipEntry(entryName);
zos.putNextEntry(zipEntry);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
fis.close();
}
}
}
}
上传:MUtiLfile file 接收数据 或者 String filePath接收
解压参考:优化方法记录里面的zip解压
流归纳
将字符串的内容作为流发送:
//降sql读取到该本地中去
String sql = "select * from user limit 10";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sql.getBytes());
int length = 0 ;
byte[] arr = new byte[1024];
String fileName = "./"+ UUID.randomUUID()+".sql";
// 除了文件流,也可以是response中的 输出流
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
while ( (length = byteArrayInputStream.read(arr)) != -1 ){
fileOutputStream.write(arr,0,length);
}
//关闭流
byteArrayInputStream.close();
fileOutputStream.close();
//降sql读取到该本地中去
String sql = "select * from user limit 10";
StringReader stringReader = new StringReader(sql);
FileWriter fileWriter = new FileWriter("./test05.sql");
br = new BufferedReader(stringReader )
while (String valueString;(valueString = br.readLine()) != null) {
//xxxx
result.append(valueString) ;
}
将流的数据转化为字符串:
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "utf-8"));
request.getInputStream() 也可以这样获取
request.getReader(); 也可以这样获取
StringBuilder result = new StringBuilder();
while (String valueString;(valueString = br.readLine()) != null) {
//xxxx
result.append(valueString) ;
}
} catch (Exception e) {
log.error("xxxx", e);
}finally {
if (null != br) {
try {
br.close();
} catch (Exception e) {
log.error("xxxx", e);
}
}
}
字符字节流,前升后降
下载远程服务器的Io
HttpURLConnection urlCon = null;
String lyric = "";
try {
URL urlAttachment = new URL(lrcUrl);
urlCon = (HttpURLConnection)urlAttachment.openConnection();
urlCon.setConnectTimeout(1000);
urlCon.setReadTimeout(1000);
} catch (IOException e) {
log.error("xxxx"+lrcUrl, e);
return lyric;
}
BufferedReader br = null;
try {
String valueString = null;
br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "utf-8"));
while ((valueString = br.readLine()) != null) {
//xxxx
lyric += valueString;
}
} catch (Exception e) {
log.error("xxxx", e);
}finally {
if (null != br) {
try {
br.close();
} catch (Exception e) {
log.error("xxxx", e);
}
}
}
return lyric;
}
--------------
urlAttachment = new URL(attachmentUrl);
List<String> lyric = IOUtils.readLines(urlAttachment.openStream(), "utf-8");
浏览器下载需要设置两个头 responese .setHeader() 一个设置返回的文件类型
@ResponseBody
......
FileInputStream fis = new FileInputStream(path);
//告知客户端要下载的文件
// filename 这里可以写中文,比如写:下载.sql
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
// 这里在下面的mimetype中找对应的文件的格式
response.setHeader("content-type", "application/文件格式");
//创建字节输出流
ServletOutputStream sos = response.getOutputStream();
//执行输出操作
int len = 1;
byte[] b = new byte[1024];
while((len = fis.read(b)) != -1) {
sos.write(b, 0, len);
}
/*方法内可以不关流*/
sos.close();
//另外一种输出
//需要设置文件返回的类型,在响应头里面,浏览器才知道是什么
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
// 这里在下面的mimetype中找对应的文件的格式
response.setHeader("content-type", "application/文件格式");
response.getOutputStream().println(str); //
text/plain 表示文本文件的默认值。
application/octet-stream 表示所有其他情况的默认值
https://blog.csdn.net/qq_39007603/article/details/128566928
https://blog.csdn.net/woacptp/article/details/80199713
一、测试
package Sort;
import com.sun.deploy.util.StringUtils;
import org.omg.IOP.Encoding;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Consumer;
public class Sort {
public static void main(String[] args) throws IOException {
//test01();
//test02();
//test03();
// test04();
test06();
}
public static void test01(){
Integer[] ints = new Integer[]{2,3,45,20,12321,1};
// Arrays.sort(ints,Collections.reverseOrder());
// Collections.sort(Arrays.asList(ints), new Comparator<int[]>() {
// @Override
// public int compare(int[] o1, int[] o2) {
// return 0;
// }
// });
//lambda
// Arrays.stream(ints).forEach(value -> System.out.println(value));
//o1-o2 是升序
//o2-o1 是降序
//lambda
Arrays.stream(ints).sorted(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1; //是什么排序??????
}
}).forEach(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
});
}
public static void test02(){
List<Student> list = new ArrayList<>();
list.add(new Student(1, "A", 20, 180));
list.add(new Student(2, "B", 23, 175));
list.add(new Student(3, "C", 22, 190));
list.add(new Student(4, "D", 21, 175));
list.add(new Student(5, "E", 20, 185));
list.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//前升后降
//身高升序,年龄升序
if(o1.getHeight() == o2.getHeight()){
return o1.getAge()-o2.getAge();
}else{
return o1.getHeight() - o2.getHeight();
}
}
}).forEach(new Consumer<Student>() {
@Override
public void accept(Student student) {
System.out.println(student);
}
});
}
public static void test03(){
System.out.println("_wrhwiefwi".matches("^([A-Za-z0-9_]{1,})$"));
}
public static void test04() throws IOException {
//降sql读取到该本地中去
String sql = "select * from user limit 10";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sql.getBytes());
int length = 0 ;
byte[] arr = new byte[1024];
String fileName = "./"+ UUID.randomUUID()+".sql";
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
while ( (length = byteArrayInputStream.read(arr)) != -1 ){
fileOutputStream.write(arr,0,length);
}
//关闭流
byteArrayInputStream.close();
fileOutputStream.close();
}
public static void test05() throws IOException {
//降sql读取到该本地中去
String sql = "select * from user limit 10";
StringReader stringReader = new StringReader(sql);
FileWriter fileWriter = new FileWriter("./test05.sql");
int length = 0;
char[] chars = new char[1024];
while( (length = stringReader.read(chars)) != -1 ){
fileWriter.write(chars,0,length);
}
stringReader.close();
fileWriter.close();
}
public static void test06() throws IOException {
FileReader reader = new FileReader("./test05.sql");
BufferedReader bufferedReader = new BufferedReader(reader);
FileWriter writer = new FileWriter("./test06.sql",true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String name = "";
while ( (name = bufferedReader.readLine()) != null ){
bufferedWriter.write(name);
bufferedWriter.newLine(); //添加
System.out.println(name);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
}
}