2013-09-21
展开全部
注:class JavaBean:Picinfo{uuid【辅助信息】、piccontent【图片介绍】、picsate【辅助信息】、sid【辅助信息】、Picpath【图片路径】} package com.xhy.action; import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext; //import class javabeanimport com.t59.model.dto.Picinfo; /**
* 文件上传
*
* @author 小月
*
*/
public class Upload {
/**
* 上传图片
*
* @param request
* @param path 图片上传路径
* @return picture infomation * @throws Exception
*/
public static Picinfo upLoadFile(HttpServletRequest request,String path)
throws Exception {//设置编码 当然 你可以使用过滤器filter request.setCharacterEncoding("gbk");
RequestContext requestContext = new ServletRequestContext(request);// 得到图片信息
Picinfo pic = new Picinfo();
// System.out.println(path);
if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory();
// 得到新的文件路径
factory.setRepository(new File("c:/"));
// factory.setRepository(new File(path)); //具体上传 ServletFileUpload upload = new ServletFileUpload(factory);//上传对象
upload.setSizeMax(2000000);//文件大小
List items = new ArrayList();//创建数组, 接收多图片信息
items = upload.parseRequest(request);
Iterator it = items.iterator();//迭代数组
while (it.hasNext()) {
FileItem fileItem = (FileItem) it.next();
if (fileItem.isFormField()) {//得到文本对象
String fieldName = fileItem.getFieldName();
if(fieldName.equals("uuid")){
pic.setUuid(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}else if(fieldName.equals("piccontent")){
pic.setPiccontent(new String(fileItem.getString().getBytes("iso8859-1"), "gbk"));
}else if(fieldName.equals("picsate")){
pic.setPicsate(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}else if(fieldName.equals("sid")){
pic.setSid(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}
} else {
//设置上传路径及文件名/*注:这里为了避免图片上传文件名重复,所以,使用时间来重命名图片*/
if (fileItem.getName() != null && fileItem.getSize() != 0) {
String filenameTemp = new Date().getTime()+".jpg";
pic.setPicpath(path+filenameTemp);
File newFile = new File(path +filenameTemp);
fileItem.write(newFile);
} else {
System.out.println("文件没有选择 或 文件内容为空");
}
} }
}// System.out.println("上传成功");
return pic;
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext; //import class javabeanimport com.t59.model.dto.Picinfo; /**
* 文件上传
*
* @author 小月
*
*/
public class Upload {
/**
* 上传图片
*
* @param request
* @param path 图片上传路径
* @return picture infomation * @throws Exception
*/
public static Picinfo upLoadFile(HttpServletRequest request,String path)
throws Exception {//设置编码 当然 你可以使用过滤器filter request.setCharacterEncoding("gbk");
RequestContext requestContext = new ServletRequestContext(request);// 得到图片信息
Picinfo pic = new Picinfo();
// System.out.println(path);
if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory();
// 得到新的文件路径
factory.setRepository(new File("c:/"));
// factory.setRepository(new File(path)); //具体上传 ServletFileUpload upload = new ServletFileUpload(factory);//上传对象
upload.setSizeMax(2000000);//文件大小
List items = new ArrayList();//创建数组, 接收多图片信息
items = upload.parseRequest(request);
Iterator it = items.iterator();//迭代数组
while (it.hasNext()) {
FileItem fileItem = (FileItem) it.next();
if (fileItem.isFormField()) {//得到文本对象
String fieldName = fileItem.getFieldName();
if(fieldName.equals("uuid")){
pic.setUuid(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}else if(fieldName.equals("piccontent")){
pic.setPiccontent(new String(fileItem.getString().getBytes("iso8859-1"), "gbk"));
}else if(fieldName.equals("picsate")){
pic.setPicsate(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}else if(fieldName.equals("sid")){
pic.setSid(Integer.parseInt(new String(fileItem.getString().getBytes("iso8859-1"), "gbk")));
}
} else {
//设置上传路径及文件名/*注:这里为了避免图片上传文件名重复,所以,使用时间来重命名图片*/
if (fileItem.getName() != null && fileItem.getSize() != 0) {
String filenameTemp = new Date().getTime()+".jpg";
pic.setPicpath(path+filenameTemp);
File newFile = new File(path +filenameTemp);
fileItem.write(newFile);
} else {
System.out.println("文件没有选择 或 文件内容为空");
}
} }
}// System.out.println("上传成功");
return pic;
}
}
2013-09-21
展开全部
我个人认为文件上传用第三方的包比较好用jspsmartupload在servlet中调用jspsmartupload,在jsp中只需要写个上传的表单就行了,javabean不需要用到吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-21
展开全部
JSP的文件上传一般都是采用SmartUpload、COS、Commos-fileupload等上传组件的。。。具体的代码也是也在Servlet中,其实这些代码本身并没有什么难度,你只要在百度或者google上搜索下“JSP文件上传”就有很多答案了,然后自己分析下就可以使用了的。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询