ActiveX控件多图片同时上传    

 

 

 

 

 

 

 

 

 

ActiveX控件多图片同时上传,采用控件与Server交互,可以同时上传多个图片,有进度条等功能。

以下是upload.jsp文件源码范例,附件有界面截图:

<%@ page language='java' import='java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*,org.apache.commons.fileupload.*,org.w3c.dom.*,org.w3c.dom.ls.*'%>
<%!
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
String galleryPath = "/Gallery/";
 
String absGalleryPath;
String absThumbnailsPath;
String absTempPath;
 
//This method verifies whether file with such name already exists
//and if so, construct safe filename name (to avoid collision).
String getSafeFileName(String fileName) {
    String newFileName = fileName;
    File file = new File(absGalleryPath + File.separator + newFileName);
    int j = 1;
    while (file.exists()) {
	newFileName = j + "_" + fileName;
	file = new File(absGalleryPath + File.separator + newFileName);
	j = j + 1;
    }
    return newFileName;
}
 
private void deleteFiles(String path) {
    File dir = new File(path);
 
    String[] dirList = dir.list();
    for (int i = 0; i < dirList.length; i++) {
	File file = new File(path + File.separator + dirList[i]);
	if (file.isFile()) {
	    file.delete();
	}
    }
}
 
private void saveXml(Document document, String path) throws IOException {
    DOMImplementation implementation = document.getImplementation();
    DOMImplementationLS implementationLS = (DOMImplementationLS) implementation.getFeature("LS", "3.0");
    LSSerializer serializer = implementationLS.createLSSerializer();
    LSOutput output = implementationLS.createLSOutput();
    FileOutputStream stream = new FileOutputStream(path);
    output.setByteStream(stream);
    serializer.write(document, output);
    stream.close();
}
%>
<%
//Process request.
ServletContext context = getServletContext();
absGalleryPath = context.getRealPath(galleryPath);
absThumbnailsPath = context.getRealPath(galleryPath + "/Thumbnails");
absTempPath = context.getRealPath(galleryPath + "/Temp");
 
//First of all, clear files and data uploaded at previous time.
 
//Delete source files.
deleteFiles(absGalleryPath);
 
//Delete thumbnails.
deleteFiles(absThumbnailsPath);
 
//NOTE: If you do not want to delete previously uploaded files, just
//remove or comment out the code above.
 
//Process upload.
DiskFileUpload fu = new DiskFileUpload();
//Set maximum size before a FileUploadException will be thrown.
fu.setSizeMax(100000000);
//Set maximum size that will be stored in memory.
fu.setSizeThreshold(4096);
//Set the location for saving data that is larger than getSizeThreshold().
fu.setRepositoryPath(absTempPath);
 
//Get uploaded files.
List listFileItems = fu.parseRequest(request);
 
//Put them in hash table for fast access.
Hashtable fileItems = new Hashtable();
 
for (int i = 0; i < listFileItems.size(); i++) {
    FileItem fileItem = (FileItem)(listFileItems.get(i));
    fileItems.put(fileItem.getFieldName(), fileItem);
}
 
//Create XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
javax.xml.parsers.DocumentBuilder builder =
	javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document descriptions = builder.newDocument();
descriptions.appendChild(descriptions.createElement("files"));
 
//Get total number of uploaded files (all files are uploaded in a single package).
int fileCount = Integer.parseInt(((FileItem) fileItems.get("FileCount")).getString());
 
//This value will keep a total size of actually uploaded images. It will be displayed
//so that you could compare how many bytes you save. 
long totalUploadedFileSize = 0;
 
//Iterate through uploaded data and save the original file, thumbnail, and description.
for (int i = 1; i <= fileCount; i++) {
    //Get the first thumbnail (watermarked image) and save it to disk.
    FileItem thumbnail1FileItem = (FileItem) fileItems.get("Thumbnail1_" + Integer.toString(i));
    String fileName = getSafeFileName(new File(((FileItem) fileItems.get("FileName_" + Integer.toString(i))).getString()).getName());
    File thumbnail1File = new File(absGalleryPath + File.separator + fileName);
    thumbnail1FileItem.write(thumbnail1File);
    
    totalUploadedFileSize += thumbnail1File.length();
    
    //Get the second thumbnail (small thumbnail) and save it to disk.
    FileItem thumbnail2FileItem = (FileItem) fileItems.get("Thumbnail2_" + Integer.toString(i));
    File thumbnail2File = new File(absThumbnailsPath + File.separator + fileName + ".jpg");
    thumbnail2FileItem.write(thumbnail2File);    
    
    //Save file info.
    Element xmlFile = descriptions.createElement("file");
    xmlFile.setAttribute("name", fileName);
    xmlFile.setAttribute("width", ((FileItem) fileItems.get("Width_" + i)).getString());
    xmlFile.setAttribute("height", ((FileItem) fileItems.get("Height_" + i)).getString());
    descriptions.getDocumentElement().appendChild(xmlFile);
}
 
//Write both optimized and source file sizes for comparison.
descriptions.getDocumentElement().setAttribute("totalSourceFileSize", ((FileItem) fileItems.get("TotalSourceFileSize")).getString());
descriptions.getDocumentElement().setAttribute("totalUploadedFileSize", Integer.toString((int)(Math.ceil(totalUploadedFileSize / 1024))));
 
saveXml(descriptions, absGalleryPath + File.separator + "Descriptions.xml");
%>

关联文档