我写过一个上传,用于上传管理员向远程服器上传数据 package com.niit.servlet;import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;import com.niit.bookshop.bean.Books; import com.niit.bookshop.dao.BooksDAO; public class UploadServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);
} /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Books book = new Books();
DiskFileItemFactory factory = new DiskFileItemFactory();
File path = new File(this.getServletContext().getRealPath("images\\")); ServletFileUpload fileUpload = new ServletFileUpload(factory); fileUpload.setFileSizeMax(1024*1024*20); //fileUpload.setHeaderEncoding("gb2312"); List<FileItem> list = null; //FileOutputStream fos = new FileOutputStream(); try{ list = fileUpload.parseRequest(request); for(FileItem item : list){ if(item.isFormField()){ //String s = new String(item.getString().getBytes("iso-8859-1"),"gb2312"); //System.out.println(item.getFieldName() + "=" + item.getString()); if(item.getFieldName().equals("bookAuthor")){ book.setBookAuthor(item.getString()); } if(item.getFieldName().equals("bookName")){ book.setBookName(item.getString()); } if(item.getFieldName().equals("bookPublish")){ book.setBookPublish(item.getString()); } if(item.getFieldName().equals("bookPrice")){ book.setBookPrice(Float.parseFloat(item.getString())); } if(item.getFieldName().equals("bookQuantity")){ book.setBookQuantity(Integer.parseInt(item.getString())); } if(item.getFieldName().equals("description")){ book.setBookDescription(item.getString()); } if(item.getFieldName().equals("type_id")){ book.setType_id(Long.parseLong(item.getString())); } }else{ if(item.isInMemory()){ String fileName = item.getName().substring(item.getName().lastIndexOf("\\") + 1); String filepath = getServletContext().getRealPath("images\\") + "\\" + fileName; book.setBookPic(fileName); FileOutputStream fos = new FileOutputStream(filepath); fos.write(item.get()); fos.close(); }else{ String fileName = item.getName().substring(item.getName().lastIndexOf("\\")); String filepath = getServletContext().getRealPath("images\\") + "\\" + fileName; book.setBookPic(fileName); FileOutputStream fos = new FileOutputStream(filepath); byte[] buff = new byte[1024*4]; InputStream is = item.getInputStream(); int size = 0; while((size = is.read(buff)) != -1){ fos.write(buff,0,size); } is.close(); fos.close(); } } } }catch(Exception e ){ e.printStackTrace(); } BooksDAO dao = new BooksDAO(); if(dao.insertBook(book)){ response.sendRedirect("success.jsp"); }else{ response.sendRedirect("error.jsp"); } }}
AJAX+缓存文件,传几百M没问题 File tempFile = new File(tempfile); FileOutputStream saveTemp = new FileOutputStream(tempFile); InputStream fileSource = request.getInputStream(); HttpSession s = ((HttpServletRequest)request).getSession(true); byte[] b = new byte[4096]; long count = 0; try{ s.setAttribute("file_status","up"); } catch(Exception e){ s.putValue("file_status","up"); } while((p = fileSource.read(b)) != -1) { saveTemp.write(b, 0, p); count += p; try{ s.setAttribute("file_size",count+""); } catch(Exception e){ s.setAttribute("file_size",count+""); } } saveTemp.close(); saveTemp=null; fileSource.close();fileSource=null; //分析临时文件 ......
web页面上传很容易出现问题。
/*****
*
*author:East(张栋芳)
*date: 2008-7-19
*note: 上传文件和下载文件
*/public class Upload {
/***
*
*上传文件平
*/
public void upLoad(){
// 新建一个SmartUpload对象
SmartUpload supload = new SmartUpload();
// 上传初始化
supload.initialize(pageContext);
// 限制每个上传文件的最大长度
supload.setMaxFileSize(10000);
// 限制总上传数据的长度。
supload.setTotalMaxFileSize(20000);
// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
supload.setAllowedFilesList("doc,txt");
// 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
supload.setDeniedFilesList("exe,bat,jsp,html,htm");
// 上传文件
supload.upload();
// 将上传文件全部保存到指定目录
int count = supload.save("/upload");
com.jspsmart.upload.SmartFile file = supload.getFiles().getFile(0);
//文件名
String fileName = file.getFieldName();
}
/***
*
*下载文件
*/
public void downLoad(){
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开 su.setContentDisposition(null);
su.downloadFile("/upload/test.doc");
}
}
我在本机上试,100多M还可以,但大于230M(不是很确)就有问题了
package com.niit.servlet;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;import com.niit.bookshop.bean.Books;
import com.niit.bookshop.dao.BooksDAO;
public class UploadServlet extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Books book = new Books();
DiskFileItemFactory factory = new DiskFileItemFactory();
File path = new File(this.getServletContext().getRealPath("images\\"));
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setFileSizeMax(1024*1024*20);
//fileUpload.setHeaderEncoding("gb2312");
List<FileItem> list = null;
//FileOutputStream fos = new FileOutputStream();
try{
list = fileUpload.parseRequest(request);
for(FileItem item : list){
if(item.isFormField()){
//String s = new String(item.getString().getBytes("iso-8859-1"),"gb2312");
//System.out.println(item.getFieldName() + "=" + item.getString());
if(item.getFieldName().equals("bookAuthor")){
book.setBookAuthor(item.getString());
}
if(item.getFieldName().equals("bookName")){
book.setBookName(item.getString());
}
if(item.getFieldName().equals("bookPublish")){
book.setBookPublish(item.getString());
}
if(item.getFieldName().equals("bookPrice")){
book.setBookPrice(Float.parseFloat(item.getString()));
}
if(item.getFieldName().equals("bookQuantity")){
book.setBookQuantity(Integer.parseInt(item.getString()));
}
if(item.getFieldName().equals("description")){
book.setBookDescription(item.getString());
}
if(item.getFieldName().equals("type_id")){
book.setType_id(Long.parseLong(item.getString()));
}
}else{
if(item.isInMemory()){
String fileName = item.getName().substring(item.getName().lastIndexOf("\\") + 1);
String filepath = getServletContext().getRealPath("images\\") + "\\" + fileName;
book.setBookPic(fileName);
FileOutputStream fos = new FileOutputStream(filepath);
fos.write(item.get());
fos.close();
}else{
String fileName = item.getName().substring(item.getName().lastIndexOf("\\"));
String filepath = getServletContext().getRealPath("images\\") + "\\" + fileName;
book.setBookPic(fileName);
FileOutputStream fos = new FileOutputStream(filepath);
byte[] buff = new byte[1024*4];
InputStream is = item.getInputStream();
int size = 0;
while((size = is.read(buff)) != -1){
fos.write(buff,0,size);
}
is.close();
fos.close();
}
}
}
}catch(Exception e ){
e.printStackTrace();
}
BooksDAO dao = new BooksDAO();
if(dao.insertBook(book)){
response.sendRedirect("success.jsp");
}else{
response.sendRedirect("error.jsp");
}
}}
FileOutputStream saveTemp = new FileOutputStream(tempFile);
InputStream fileSource = request.getInputStream();
HttpSession s = ((HttpServletRequest)request).getSession(true);
byte[] b = new byte[4096];
long count = 0;
try{
s.setAttribute("file_status","up");
}
catch(Exception e){
s.putValue("file_status","up");
}
while((p = fileSource.read(b)) != -1)
{
saveTemp.write(b, 0, p);
count += p;
try{
s.setAttribute("file_size",count+"");
}
catch(Exception e){
s.setAttribute("file_size",count+"");
}
}
saveTemp.close(); saveTemp=null;
fileSource.close();fileSource=null;
//分析临时文件
......
此控件支持100G文件的断点续传操作,提供了完善的开发文档,支持文件MD5验证,支持文件批量上传。
粘贴文件,简化选择文件操作:文件MD5值计算进度:文件MD5值计算完毕服务器根据MD5检测是否存在相同文件续传文件从服务器加载文件列表文件上传中文件上传完毕上传文件夹与Discuz!X2整合-后台安装断点续传控件与Discuz!X2整合-后台启用断点续传控件与Discuz!X2整合-后台断点续传控件启用成功与Discuz!X2整合-前台发帖页面与Discuz!X2整合-上传