上传文件到服务器
创建一个类用于保存上传的文件的信息
/** * @Description: 解析上传文件信息,包括获取文件大小,文件流,文件名字等 */ public class UploadFileStatus { /*文件名字,该名字需遵守命名规范,改名字会直接作为静态文件服务器上的名字*/ private String fileName; /*文件类型,包括img,video,html,preview_html等*/ private String fileType; /*文件路径,用于提示文件服务器将文件存储到何种路径之下*/ private String filePath; /*文件的输出流*/ private InputStream inputStream; /*文件的大小*/ private long fileSize; private boolean ifStatic; public boolean isIfStatic() { return ifStatic; } public void setIfStatic(boolean ifStatic) { this.ifStatic = ifStatic; } public String getFileName() { return fileName; } public UploadFileStatus setFileName(String fileName) { this.fileName = fileName; return this; } public String getFileType() { return fileType; } public UploadFileStatus setFileType(String fileType) { this.fileType = fileType; return this; } public String getFilePath() { return filePath; } public UploadFileStatus setFilePath(String filePath) { this.filePath = filePath; return this; } public InputStream getInputStream() { return inputStream; } public UploadFileStatus setInputStream(InputStream inputStream) { this.inputStream = inputStream; return this; } public long getFileSize() { return fileSize; } public UploadFileStatus setFileSize(long fileSize) { this.fileSize = fileSize; return this; } @Override public String toString() { return "UploadFileStatus{" + "fileName='" + fileName + '\'' + ", fileType='" + fileType + '\'' + ", filePath='" + filePath + '\'' + ", fileSize=" + fileSize + ", ifStatic=" + ifStatic + '}'; } // 工厂方法 public static UploadFileStatus of(String fileName, String fileType, String filePath, InputStream inputStream) { return new UploadFileStatus().setFileName(fileName) .setFileType(fileType) .setFilePath(filePath) .setInputStream(inputStream); } }
将文件发送到服务器的工具类
/** * @Description: 用于发送各种Http请求的工具类 */ public class HttpUtils { /** * @Description: 用于向静态资源服务器推送数据流(包括图片,视频等静态资源) * @param targetUrl 目标URL * @param status 上传的文件信息 * @param type 文件类型 */ // 发送post请求,获取String public static String postFile(String targetUrl, UploadFileStatus status, String type) { try { URL url = new URL(targetUrl.trim()); // 打开连接 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // 设置允许输入输出 urlConnection.setDoOutput(true); urlConnection.setDoInput(true); // 设置不用缓存 urlConnection.setUseCaches(false); // 设置传递方式 urlConnection.setRequestMethod("POST"); // 设置维持长连接 //urlConnection.setRequestProperty("Connection", "Keep-Alive"); // 设置字符集 urlConnection.setRequestProperty("Charset", "UTF-8"); // 设置文件长度和类型 if(!ValidatorUtils.isEmpty(status.getFileSize())){ urlConnection.setRequestProperty("Content-Length", String.valueOf(status.getFileSize())); } urlConnection.setRequestProperty("Connection", "keep-alive"); urlConnection.setRequestProperty("Content-Type", "application/x-form-urlencoded"); urlConnection.setRequestProperty("fileName", status.getFileName()); urlConnection.setRequestProperty("fileType", status.getFileType()); urlConnection.setRequestProperty("filePath", status.getFilePath()); urlConnection.setRequestProperty("uploadType", String.valueOf(type)); // 开始连接请求 urlConnection.connect(); OutputStream out = urlConnection.getOutputStream(); // 获取上传文件的输入流 InputStream fileIS = status.getInputStream(); byte[] tempBytes = new byte[1024]; int byteRead = 0; // 写入请求的字符串 while((byteRead = fileIS.read(tempBytes)) != -1) { out.write(tempBytes, 0, byteRead); } out.flush(); fileIS.close(); out.close(); //TODO System.out.println(status.getFileName() +"." + status.getFileType() + "的上传回复的code是:" + urlConnection.getResponseCode()); // 请求返回的状态 if (200 == urlConnection.getResponseCode()) { // 关闭数据流 status.getInputStream().close(); // 请求返回的数据 InputStream in = urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = in.read(buffer))) { baos.write(buffer, 0, len); baos.flush(); } return baos.toString("utf-8"); } } catch (IOException e) { e.printStackTrace(); return "fail"; } return "fail"; } }