http 二进制流上传和下载zip包文件入库 java代码
时间: 2023-12-04 22:05:17 浏览: 129
上传二进制流:
```java
public void uploadFile(byte[] fileBytes, String fileName) {
try {
String url = "http://example.com/upload"; // 上传文件的接口URL
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/zip"); // 设置上传文件的文件类型
conn.setRequestProperty("Content-Disposition", "attachment;filename=\"" + fileName + "\""); // 设置上传文件的文件名
OutputStream outputStream = conn.getOutputStream();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// 上传成功
} else {
// 上传失败
}
} catch (Exception e) {
e.printStackTrace();
}
}
```
下载二进制流:
```java
public byte[] downloadFile(String fileUrl) {
byte[] fileBytes = null;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(fileUrl).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
fileBytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
inputStream.close();
} else {
// 下载失败
}
} catch (Exception e) {
e.printStackTrace();
}
return fileBytes;
}
```
将二进制流写入文件:
```java
public void writeBytesToFile(byte[] fileBytes, String filePath) {
try {
FileOutputStream outputStream = new FileOutputStream(filePath);
outputStream.write(fileBytes);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
```
将文件读取为二进制流:
```java
public byte[] readFileToBytes(String filePath) {
byte[] fileBytes = null;
try {
FileInputStream inputStream = new FileInputStream(filePath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
fileBytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileBytes;
}
```
将二进制流存入数据库:
```java
public void saveBytesToDatabase(byte[] fileBytes, String fileName) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
conn = DriverManager.getConnection(url, "root", "password");
String sql = "INSERT INTO files (name, content) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fileName);
pstmt.setBytes(2, fileBytes);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
从数据库读取二进制流:
```java
public byte[] readBytesFromDatabase(String fileName) {
byte[] fileBytes = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
conn = DriverManager.getConnection(url, "root", "password");
String sql = "SELECT content FROM files WHERE name = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fileName);
rs = pstmt.executeQuery();
if (rs.next()) {
fileBytes = rs.getBytes("content");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return fileBytes;
}
```
阅读全文