|
@@ -0,0 +1,223 @@
|
|
|
|
|
+package com.mrxu.framework.common.util;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
|
+import java.net.URL;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+public class FileFunc {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 文件里面写内容
|
|
|
|
|
+ * @param filePath
|
|
|
|
|
+ * @param fileName
|
|
|
|
|
+ * @param sb
|
|
|
|
|
+ * @param isadd
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void write(String filePath,String fileName,String sb,boolean isadd) {
|
|
|
|
|
+ File fp = new File(filePath);
|
|
|
|
|
+ if (!fp.exists()) {
|
|
|
|
|
+ fp.mkdirs();//目录不存在的情况下,创建目录。
|
|
|
|
|
+ }
|
|
|
|
|
+ FileWriter writer = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ writer = new FileWriter(filePath+fileName,isadd);
|
|
|
|
|
+ writer.write(sb.toString());
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(writer != null) {
|
|
|
|
|
+ writer.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static byte[] getBytes(String filePath) {
|
|
|
|
|
+ byte[] buffer = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
|
|
|
|
+ byte[] b = new byte[1024];
|
|
|
|
|
+ int n;
|
|
|
|
|
+ while ((n = fis.read(b)) != -1) {
|
|
|
|
|
+ bos.write(b, 0, n);
|
|
|
|
|
+ }
|
|
|
|
|
+ fis.close();
|
|
|
|
|
+ bos.close();
|
|
|
|
|
+ buffer = bos.toByteArray();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (FileNotFoundException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return buffer;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据byte数组,生成文件
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void saveFile(byte[] bfile, String filePath, String fileName) {
|
|
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
|
|
+ FileOutputStream fos = null;
|
|
|
|
|
+ File file = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ File dir = new File(filePath);
|
|
|
|
|
+ if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
|
|
|
|
|
+ dir.mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ file = new File(filePath + "\\" + fileName);
|
|
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
|
|
+ bos = new BufferedOutputStream(fos);
|
|
|
|
|
+ bos.write(bfile);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ if (bos != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ bos.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e1) {
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fos != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fos.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e1) {
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取文件后缀名
|
|
|
|
|
+ * @param file
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getFileType(File file) {
|
|
|
|
|
+ String name = file.getName();
|
|
|
|
|
+ return name.substring(name.lastIndexOf(".")+1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载网络文件,用U拍云图片下载
|
|
|
|
|
+ */
|
|
|
|
|
+ public static File download(String urlString,String path) {
|
|
|
|
|
+ File outFile = new File(path);
|
|
|
|
|
+ if(!outFile.exists()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ outFile.createNewFile();
|
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ URL url = null;
|
|
|
|
|
+ OutputStream os = null;
|
|
|
|
|
+ InputStream is = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ url = new URL(urlString);
|
|
|
|
|
+ os = new FileOutputStream(outFile);
|
|
|
|
|
+ is = url.openStream();
|
|
|
|
|
+ byte[] buff = new byte[1024];
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ int count = is.read(buff);
|
|
|
|
|
+ if (count == -1) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ os.write(buff, 0, count);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (is != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ is.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (os != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ os.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return outFile;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String uploadFile(String urlStr,String type,File file) {
|
|
|
|
|
+ String res = "";
|
|
|
|
|
+ HttpURLConnection conn = null;
|
|
|
|
|
+ String BOUNDARY = "---------------------------123821742118716";
|
|
|
|
|
+ try {
|
|
|
|
|
+ URL url = new URL(urlStr);
|
|
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
|
|
+ conn.setReadTimeout(30000);
|
|
|
|
|
+ conn.setDoOutput(true);
|
|
|
|
|
+ conn.setDoInput(true);
|
|
|
|
|
+ conn.setUseCaches(false);
|
|
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
|
|
+ conn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
|
|
+ conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
|
|
+ conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
|
|
|
|
|
+ OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
|
|
+ strBuf.append("Content-Disposition: form-data;name=\"media\";filename=\"example.png\"\r\n");
|
|
|
|
|
+ strBuf.append("Content-Type:application/octet-stream\r\n\r\n");
|
|
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
|
|
+ int bytes = 0;
|
|
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
|
|
+ FileInputStream in = new FileInputStream(file);
|
|
|
|
|
+ while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
|
|
+ }
|
|
|
|
|
+ in.close();
|
|
|
|
|
+ byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
|
|
+ out.write(endData);
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ // 读取返回数据
|
|
|
|
|
+ StringBuffer rs = new StringBuffer();
|
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
+ String line = null;
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
|
+ rs.append(line).append("\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ res = rs.toString();
|
|
|
|
|
+ reader.close();
|
|
|
|
|
+ reader = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ if (conn != null) {
|
|
|
|
|
+ conn.disconnect();
|
|
|
|
|
+ conn = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return res;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|