首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

大文件上传兑现

2012-06-20 
大文件上传实现?一般都会大于100M不知道大家是如何干的 谢谢咯[解决办法]package com.uploadimport com.j

大文件上传实现?
一般都会大于100M
不知道大家是如何干的 谢谢咯

[解决办法]
package com.upload;

import com.jspsmart.upload.SmartUpload;
/*****
 * 
 *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");
}

}
[解决办法]
我写过一个上传,用于上传管理员向远程服器上传数据

Java code
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");
}
}

}

热点排行