parent
a13ead3fd3
commit
b03294dbf4
@ -0,0 +1,21 @@
|
||||
package com.luoo.music.util;
|
||||
|
||||
import java.io.File;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class CommonUtilTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
MultipartFile file = getMultipartFile();
|
||||
long duration = CommonUtil.getSongDuration(file);
|
||||
System.out.println(duration);
|
||||
String format = CommonUtil.formatSongDuration(duration);
|
||||
System.out.println(format);
|
||||
}
|
||||
|
||||
private MultipartFile getMultipartFile() {
|
||||
return new FileToMultipartFile(new File("src/test/resources/song_1_1.mp3"));
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.luoo.music.util;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.*;
|
||||
|
||||
public class FileToMultipartFile implements MultipartFile {
|
||||
|
||||
private final File file;
|
||||
|
||||
public FileToMultipartFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return file.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalFilename() {
|
||||
return file.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return file.length() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return file.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, len);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transferTo(File dest) throws IOException, IllegalStateException {
|
||||
try (InputStream is = new FileInputStream(file);
|
||||
OutputStream os = new FileOutputStream(dest)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue