java原生环境,不许引用其他文件 这次废话不多说,直接上代码 各个代码块之间的代码可以单独使用
远端获取文件
URL进行远端文件下载
本人亲写,绝对有效
public class FileGainUtil {
public static void main(String[] args) {
FileGainUtil fileUtilLM = new FileGainUtil();
String[] urls = new String[]{
"https://img-blog.csdnimg.cn/img_convert/c8b0205e2b93a2c243b9c95541e83bbc.png",
"https://img-blog.csdnimg.cn/img_convert/d645dc79a2565bcb940d237807666172.png",
"https://img-blog.csdnimg.cn/img_convert/2b17c8c96f0d2ce292cae9b7ba68fb98.png",
"http://wwwweeaazzcdfdsa"
};
List<String> list = new ArrayList<>();
fileUtilLM.actionAddFile(urls,"F:/cc/imama",".jpg",list);
System.out.println(list);
String url = "https://img-blog.csdnimg.cn/img_convert/c8b0205e2b93a2c243b9c95541e83bbc.png";
String path = "F://cs/image";
String suffix = ".jpg";
String name = "照片1";
fileUtilLM.downLoadFile(url,path,suffix,name);
}
private byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
public boolean actionAddFile (String[] urls, String pathName, String suffix) {
boolean temp = true;
File file = new File(pathName);
file.mkdirs();
try {
for(int i=0;i<urls.length;i++) {
downLoadFile(urls[i],pathName,suffix,i+"");
}
} catch (Exception e) {
e.printStackTrace();
temp = false;
}
return temp;
}
public void downLoadFile(String urlName,String pathName,String suffix,String name) throws Exception {
URL url = new URL(urlName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
byte[] data = readInputStream(inStream);
File imageFile = new File(pathName+"/"+name+suffix);
FileOutputStream outStream = new FileOutputStream(imageFile);
outStream.write(data);
outStream.close();
inStream.close();
}
public void actionAddFile (String[] urls, String pathName, String suffix, List<String> errors) {
File file = new File(pathName);
file.mkdirs();
for (int i = 0; i < urls.length; i++) {
try {
downLoadFile(urls[i],pathName,suffix,i+"");
} catch (Exception e) {
errors.add(pathName+"|"+urls[i]+ "==错误信息:" +e.toString());
}
}
}
}
本地文件打包
打zip包
本人亲写,绝对有效
public class FileZipUtil {
public static void main(String[] args) {
FileZipUtil zc = new FileZipUtil("F:/Test.zip");
zc.compress("F:/cc","F:/image");
FileZipUtil zc2 = new FileZipUtil();
zc2.setZipFile("F:/Test.zip");
zc2.compress("F:/cc");
zc2.compress("F:/cc/image/1.jpg");
}
static final int BUFFER = 8192;
private File zipFile;
public FileZipUtil() {}
public FileZipUtil(String pathName) {
zipFile = new File(pathName);
}
public void setZipFile(String pathName) {
this.zipFile = new File(pathName);
}
public void setZipFile(File file) {
this.zipFile = file;
}
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i=0;i<pathName.length;i++){
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
if (file.isDirectory()) {
this.compressDirectory(file, out, basedir);
} else {
this.compressFile(file, out, basedir);
}
}
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], out, basedir + dir.getName() + "/");
}
}
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
文件下载到浏览器
http连接进行点击下载到浏览器
本人亲写,绝对有效 本代码所用技术SpringBoot 也可以用原生Sevlet技术等等…
- 可以把 @GET和@Paht(“ssoDownload/file/{zipName}”) 这两个注释换成
@RequestMapping("") … - 可以把@Context这个注释去掉
- 可以把@JaxrsParameterDescribe这个注释去掉
- @PathParam(“zipName”) 可以换成 SpringBoot常用的写法
1)服务器端代码
@GET
@Path("ssoDownload/file/{zipName}")
public void ssoDownload(@Context HttpServletResponse response,
@JaxrsParameterDescribe("需要下载的文件路径+文件名(包括后缀) 一次只能下载一个")
@PathParam("zipName") String zipName) throws IOException {
logger.info("============进行文件下载:是否删除原文件");
OutputStream ous = null;
InputStream ins = null;
File file = new File(zipName);
try {
ins = new BufferedInputStream(new FileInputStream(file));
response.reset();
ous = new BufferedOutputStream(response.getOutputStream());
byte[] bf = new byte[1024*8];
int i = 0;
while ((i = ins.read(bf)) != -1) {
ous.write(i);
}
ous.flush();
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
} catch (IOException e) {
e.printStackTrace();
} finally {
ins.close();
ous.close();
}
}
2)浏览器端代码
window.Open("上方的请求地址 如: localhost:80/ssoDownload/file/{zipName}");
本地文件操作
删除目录
public class FileDeleteUtil {
public static void delFile(File index){
if (index.isDirectory()){
File[] files = index.listFiles();
for (File in: files) {
delFile(in);
}
}
index.delete();
}
public static void delFile(String... fileNames) {
for (int i = 0; i < fileNames.length; i++) {
delFile(new File(fileNames[i]));
}
}
public static void delFile(String fileName) {
delFile(new File(fileName));
}
}
|