上傳大文件經(jīng)常遇到上傳一半由于網(wǎng)絡(luò)或者其他一些原因上傳失敗摧扇。然后又得重新上傳(很麻煩),所以就想能不能做個斷點上傳的功能殴泰。于是網(wǎng)上搜索于宙,發(fā)現(xiàn)市面上很少有斷點上傳的案例,有找到一個案例也是采用SOCKET作為上傳方式(大文件上傳悍汛,不適合使用POST,GET形式)捞魁。由于大文件夾不適合http上傳的方式,所以就想能不能把大文件切割成n塊小文件离咐,然后上傳這些小文件谱俭,所有小文件全部上傳成功后再在服務(wù)器上進(jìn)行拼接。這樣不就可以實現(xiàn)斷點上傳健霹,又解決了http不適合上傳大文件的難題了嗎M稀!糖埋!
服務(wù)端:
需要寫兩個接口
接口一:根據(jù)上傳文件名稱filename 判斷是否之前上傳過宣吱,沒有則返回客戶端chunck=1,有則讀取記錄chunck并返回瞳别。
接口二:上傳文件征候,如果上傳塊數(shù)chunck=chuncks,遍歷所有塊文件拼接成一個完整文件祟敛。
接口一代碼如下:
@WebServlet(urlPatterns = { "/ckeckFileServlet" })
public class CkeckFileServlet extends HttpServlet {
private FileUploadStatusServiceI statusService;
String repositoryPath;
String uploadPath;
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext servletContext = config.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
statusService = (FileUploadStatusServiceI) context.getBean("fileUploadStatusServiceImpl");
repositoryPath = FileUtils.getTempDirectoryPath();
uploadPath = config.getServletContext().getRealPath("datas/uploader");
File up = new File(uploadPath);
if (!up.exists()) {
up.mkdir();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String fileName = new String(req.getParameter("filename"));
//String chunk = req.getParameter("chunk");
//System.out.println(chunk);
System.out.println(fileName);
resp.setContentType("text/json; charset=utf-8");
TfileUploadStatus file = statusService.get(fileName);
try {
if (file != null) {
int schunk = file.getChunk();
deleteFile(uploadPath + schunk + "_" + fileName);
//long off = schunk * Long.parseLong(chunkSize);
resp.getWriter().write("{\"off\":" + schunk + "}");
} else {
resp.getWriter().write("{\"off\":1}");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
接口二代碼如下:
@WebServlet(urlPatterns = { "/uploaderWithContinuinglyTransferring" })
public class UploaderServletWithContinuinglyTransferring extends HttpServlet {
private static final long serialVersionUID = 1L;
private FileUploadStatusServiceI statusService;
String repositoryPath;
String uploadPath;
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext servletContext = config.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
statusService = (FileUploadStatusServiceI) context.getBean("fileUploadStatusServiceImpl");
repositoryPath = FileUtils.getTempDirectoryPath();
System.out.println("臨時目錄:" + repositoryPath);
uploadPath = config.getServletContext().getRealPath("datas/uploader");
System.out.println("目錄:" + uploadPath);
File up = new File(uploadPath);
if (!up.exists()) {
up.mkdir();
}
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
Integer schunk = null;// 分割塊數(shù)
Integer schunks = null;// 總分割數(shù)
String name = null;// 文件名
BufferedOutputStream outputStream = null;
if (ServletFileUpload.isMultipartContent(request)) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024);
factory.setRepository(new File(repositoryPath));// 設(shè)置臨時目錄
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(5 * 1024 * 1024 * 1024);// 設(shè)置附近大小
List<FileItem> items = upload.parseRequest(request);
// 生成新文件名
String newFileName = null;
for (FileItem item : items) {
if (!item.isFormField()) {// 如果是文件類型
name = newFileName;// 獲得文件名
if (name != null) {
String nFname = newFileName;
if (schunk != null) {
nFname = schunk + "_" + name;
}
File savedFile = new File(uploadPath, nFname);
item.write(savedFile);
}
} else {
// 判斷是否帶分割信息
if (item.getFieldName().equals("chunk")) {
schunk = Integer.parseInt(item.getString());
//System.out.println(schunk);
}
if (item.getFieldName().equals("chunks")) {
schunks = Integer.parseInt(item.getString());
}
if (item.getFieldName().equals("name")) {
newFileName = new String(item.getString());
}
}
}
//System.out.println(schunk + "/" + schunks);
if (schunk != null && schunk == 1) {
TfileUploadStatus file = statusService.get(newFileName);
if (file != null) {
statusService.updateChunk(newFileName, schunk);
} else {
statusService.add(newFileName, schunk, schunks);
}
} else {
TfileUploadStatus file = statusService.get(newFileName);
if (file != null) {
statusService.updateChunk(newFileName, schunk);
}
}
if (schunk != null && schunk.intValue() == schunks.intValue()) {
outputStream = new BufferedOutputStream(new FileOutputStream(new File(uploadPath, newFileName)));
// 遍歷文件合并
for (int i = 1; i <= schunks; i++) {
//System.out.println("文件合并:" + i + "/" + schunks);
File tempFile = new File(uploadPath, i + "_" + name);
byte[] bytes = FileUtils.readFileToByteArray(tempFile);
outputStream.write(bytes);
outputStream.flush();
tempFile.delete();
}
outputStream.flush();
}
response.getWriter().write("{\"status\":true,\"newName\":\"" + newFileName + "\"}");
} catch (FileUploadException e) {
e.printStackTrace();
response.getWriter().write("{\"status\":false}");
} catch (Exception e) {
e.printStackTrace();
response.getWriter().write("{\"status\":false}");
} finally {
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
關(guān)鍵點:
1 需要與數(shù)據(jù)庫交互疤坝,數(shù)據(jù)庫存放文件名,第幾塊馆铁,總共幾塊這幾個字段
2 上傳時先調(diào)用接口一跑揉,通過文件名查詢數(shù)據(jù),如果有數(shù)據(jù)埠巨,則表示接下來要續(xù)傳历谍,接著調(diào)用第二個接口進(jìn)行上傳剩下的文件塊
3 如果沒有數(shù)據(jù)則表示是新文件上傳,調(diào)用接口二辣垒,從第一塊開始上傳望侈,接口二主要作用記錄并更新文件的上傳信息(數(shù)據(jù)庫數(shù)據(jù)),在滿足當(dāng)前上傳區(qū)塊等于總塊數(shù)時進(jìn)行合并文件勋桶。
另外數(shù)據(jù)庫新建表可以執(zhí)行如下數(shù)據(jù)庫語句
DROP TABLE IF EXISTS sys_file_upload_status
;
CREATE TABLE sys_file_upload_status
(
obj_id
varchar(36) NOT NULL,
file_name
varchar(1000) DEFAULT NULL,
chunk
int(11) DEFAULT NULL,
chunks
int(11) DEFAULT NULL,
PRIMARY KEY (obj_id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
數(shù)據(jù)庫交互大家使用自己熟悉的方式即可脱衙,比如jdbc侥猬,hibernate,mybatis等等捐韩,哪個好用用哪個退唠。