直接上代碼
[java]
@Controller
public class UploadController extends BaseController {
private static final Log log = LogFactory
private UploadService uploadService;
private AuthService authService;
/**
* 大文件分成小文件塊上傳
* 上相應的位置
* 該文件的uploadStatus為
*
*/
@RequestMapping(
@ResponseBody
public Object upload(HttpServletResponse response
@RequestParam(value =
@RequestParam(value =
@RequestParam(value =
@RequestParam(value =
@RequestParam(value =
@RequestParam(value =
@RequestParam Map<String
checkEmpty(appkey
checkEmpty(token
checkEmpty(uuid
checkEmpty(blockIndex
checkEmpty(appsig
if (multipartFile == null) {
throw new BaseException(BaseException
}
Long uuidL = parseLong(uuid
Integer blockIndexI = parseInt(blockIndex
Map<String
AccessToken accessToken = CasUtil
Long uid = accessToken
String bucketUrl = accessToken
// 從上傳目錄拷貝文件到工作目錄
String fileAbsulutePath = null;
try {
fileAbsulutePath = pyFile(multipartFile
} catch (IOException ioe) {
log
throw new BaseException(BaseException
}
File uploadedFile = new File(Global
checkEmptyFile(uploadedFile)
Object rs = uploadService
setHttpStatusOk(response)
return rs;
}
// TODO 查看下這裡是否有問題
// 上傳文件非空驗證
private void checkEmptyFile(File file) {
if (file == null || file
throw new BaseException(BaseException
}
}
/**
* 寫文件到本地文件夾
*
* @throws IOException
* 返回生成的文件名
*/
private String copyFile(InputStream inputStream
OutputStream outputStream = null;
String tempFileName = null;
int pointPosition = fileName
if (pointPosition <
tempFileName = UUID
} else {// myvedio
tempFileName = UUID
}
try {
outputStream = new FileOutputStream(Global
int readBytes =
byte[] buffer = new byte[
while ((readBytes = inputStream
outputStream
}
return tempFileName;
} catch (IOException ioe) {
// log
throw new BaseException(BaseException
} finally {
if (outputStream != null) {
try {
outputStream
} catch (IOException e) {
}
}
if (inputStream != null) {
try {
inputStream
} catch (IOException e) {
}
}
}
}
/**
* 測試此服務是否可用
*
* @param response
* @return
* @author zwq
*/
@RequestMapping(
@ResponseBody
public Object testServer(HttpServletResponse response) {
setHttpStatusOk(response)
return Global
}
public UploadService getUploadService() {
return uploadService;
}
public void setUploadService(UploadService uploadService) {
this
}
public void setAuthService(AuthService authService) {
this
}
public AuthService getAuthService() {
return authService;
}
}
比如要上傳的文件是 test
分塊依次上傳
[java]
/**
* 把所有塊從臨時文件目錄移到指定本地目錄或S
*
* @param preUpload
*/
private void moveBlockFiles(BlockPreuploadFileInfo preUpload) {
@SuppressWarnings(
String[] s
String[] localBlockUrl=new String[preUpload
List<BlockUploadInfo> blocks = (List<BlockUploadInfo>) getBaseDao()
String tempDirName = SyncUtil
String parentPath = Global
+ String
String dirPath = parentPath + Global
new File(dirPath)
int j=
for (BlockUploadInfo info : blocks) {
try {
String strBlockIndex = createStrBlockIndex(info
String suffixPath = preUpload
String tempFilePath = info
File tempFile = new File(tempFilePath)
File tmpFile = new File(dirPath + suffixPath)
if (tmpFile
FileUtils
}
FileUtils
localBlockUrl[j]=dirPath + suffixPath;
j++;
info
getBaseDao()
if (log
(preUpload
} catch (IOException e) {
log
throw new BaseException(
}
}
preUpload
preUpload
preUpload
getBaseDao()
}
private String createStrBlockIndex(int blockIndex) {
String strBlockIndex;
if (blockIndex <
strBlockIndex =
} else if (
strBlockIndex =
} else if (
strBlockIndex =
} else {
strBlockIndex =
}
return strBlockIndex;
}
最後是文件的組裝源代碼
[java]
/**
* 組裝文件
*
*/
private void assembleFileWithBlock(BlockPreuploadFileInfo preUpload) {
String dirPath = preUpload
// 開始在指定目錄組裝文件
String uploadedUrl = null;
String[] separatedFiles;
String[][] separatedFilesAndSize;
int fileNum =
File file = new File(dirPath)
separatedFiles = file
separatedFilesAndSize = new String[separatedFiles
Arrays
fileNum = separatedFiles
for (int i =
separatedFilesAndSize[i][
String fileName = dirPath + separatedFiles[i];
File tmpFile = new File(fileName)
long fileSize = tmpFile
separatedFilesAndSize[i][
}
RandomAccessFile fileReader = null;
RandomAccessFile fileWrite = null;
long alreadyWrite =
int len =
byte[] buf = new byte[
try {
uploadedUrl = Global
fileWrite = new RandomAccessFile(uploadedUrl
for (int i =
fileWrite
// 讀取
fileReader = new RandomAccessFile((dirPath + separatedFilesAndSize[i][
// 寫入
while ((len = fileReader
fileWrite
}
fileReader
alreadyWrite += Long
}
fileWrite
preUpload
preUpload
getBaseDao()
if(Global
{
//組裝完畢沒有問題 刪除掉S
String[] path=preUpload
for (String string : path) {
try {
if(Global
{
S
}else
{
S
}
} catch (Exception e) {
log
}
}
}
if (log
(preUpload
} catch (IOException e) {
log
try {
if (fileReader != null) {
fileReader
}
if (fileWrite != null) {
fileWrite
}
} catch (IOException ex) {
log
}
}
}
BlockPreuploadFileInfo 是我們自定義的業務文件處理 bean
OK
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26424.html