30/09/2018, 18:32
Cách upload multiple files sử dụng java jsp
Code này chạy tốt khi upload 1 file, cho mình hỏi cách upload nhiều files 1 lúc. Cảm ơn mọi người.
index.jsp
<%@page import="java.io.*" %>
<%-- <%@page import="java.io.File"%> --%>
<%-- <%@page import="java.io.InputStreamReader"%> --%>
<%-- <%@page import="java.net.URL"%> --%>
<%-- <%@page import="java.io.FileReader"%> --%>
<%-- <%@page import="java.io.BufferedReader"%> --%>
<%@page import="jxl.*"%>
<%-- <%@page import="jxl.read.biff.BiffException"%> --%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File upload</title>
</head>
<body>
<h1>Uploading files</h1>
<form name="uploadForm" action= "index.jsp" method="POST" enctype="multipart/form-data">
<input type="file" name="file" value="" multiple="multiple" width="100"/>
<input type="submit" name="Submit" value="submit" multiple="multiple"/>
<%
String saveFile= new String();
//get content type
String contentType=request.getContentType();
// make sure first of all not null
if((contentType!=null)&&(contentType.indexOf("multipart/form-data")>=0))
{
// begin to read a files
// creating DataInputStream
DataInputStream in = new DataInputStream(request.getInputStream());
//get content links of the request
int formDataLength= request.getContentLength();
// equal size
byte dataBytes[]= new byte[formDataLength];
int totalBytesRead=0;
int byteRead;
while(totalBytesRead<formDataLength)
{
byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=byteRead;
}
//convert it to string
String file= new String(dataBytes);
saveFile= file.substring(file.indexOf("filename="")+10);
saveFile= saveFile.substring(0,saveFile.indexOf("
"));
saveFile= saveFile.substring(saveFile.lastIndexOf("\")+1, saveFile.indexOf("""));
int lastIndex= contentType.lastIndexOf("=");
String boundary=contentType.substring(lastIndex+1, contentType.length());
int pos;
pos=file.indexOf("filename="");
pos=file.indexOf("
",pos)+1;
pos=file.indexOf("
",pos)+1;
pos=file.indexOf("
",pos)+1;
int boundaryLocation=file.indexOf(boundary,pos)-4;
// get length of position
int startPos=((file.substring(0,pos)).getBytes()).length;
int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
saveFile="C:/upload/"+saveFile;
//creating a file object
File ff = new File(saveFile);
try{
//creating a FileOutputStream object
FileOutputStream fileOut= new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, endPos-startPos);
fileOut.flush();
fileOut.close();
}
catch(Exception e){
out.print(e);
}
}
%>
</form>
</body>
</html>
Bài liên quan
nó sẽ giúp ích cho bạn
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=how%20to%20upload%20multiple%20files%20using%20jsp%20%26%20servlet
mình đọc nát luôn rồi mà k cái nào được, ức chế quá