如何上传文件
麻烦高人能说详细点
用什么组件,怎么配置,具体代码,谢谢了
我用的服务器是ORACLE+APACHE
[解决办法]
部分代码 采用apache fileUpload
List upList = new ArrayList();
try {
DiskFileUpload fu = new DiskFileUpload();
//设置最大上传尺寸
fu.setSizeThreshold(1048576); //10M
List fileItems = fu.parseRequest(request);
Iterator it = fileItems.iterator();
String fileName = null;
boolean flag = false;
while (it.hasNext()) {
FileItem fi = (FileItem) it.next();
long size = fi.getSize();
if(size > 10 * 1024 * 1024){
Map model = new HashMap();
model.put( "error ", "egis.support.taskprocess.error.A7005 ");
return new ModelAndView(ViewNames.UPLOAD_ATTACHMENTS_VIEW, model);
}
//获取文件名和内容
fileName = fi.getName();
if(fileName == null || fileName.equals( " ")){
continue;
} else {
flag = true;
}
DevLog.debug( "-------> original file name is: " + fileName);
int sindex = fileName.lastIndexOf( '\\ ');
if (sindex < 0) {
sindex = fileName.lastIndexOf( '/ ');
}
fileName = sindex < 0 ? fileName : fileName.substring(sindex + 1);
UploadAttachmentDTO dto = new UploadAttachmentDTO();
dto.setBusinessActivityId(businessActivityId);
int dindex = fileName.lastIndexOf( ". ");
dto.setAttachmentTypeCode(fileName.substring(dindex + 1).substring(0, 2));
dto.setAttachmentVersionNo( "1 ");
dto.setBusinessActivityTypeCode(businessActivityTypeCode);
dto.setAttachmentHeadline(fileName);
dto.setAttachmentDescription(fileName);
dto.setCreatedUser(userId);
String savePath = fileManager.uploadFile(fi.getInputStream(),
DirNameUtil.generateFilePath( " ", fileName, userId));
dto.setAttachmentPath(savePath);
upList.add(dto);
}
if(!flag){
throw new Exception( "没有选择文件! ");
}
[解决办法]
public ActionForward KH016_Confirm(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try
{
KH010Form thisForm = (KH010Form) form;
KH010Logic KH010Logic = new KH010Logic();
FormFile file=thisForm.getWorkhistory();
FormFile file2 = thisForm.getProjecthistory();
String workfile = file.getFileName();
String projectfile = file2.getFileName();
String uploadPath = BseSystemPropertyCopy.getInstance().getProperty( "updown_path ")+ "\\ ";
if(!(new File(uploadPath).isDirectory()))
{
new File(uploadPath).mkdir();
}
if (!workfile.equals( " "))
{
copy2(file,uploadPath);
}
if (!projectfile.equals( " "))
{
copy2(file2,uploadPath);
}
KH010Logic.updateFile(thisForm, request,workfile,projectfile);
thisForm.setCloseflg( "1 ");
}
catch (Exception exp)
{
logger.error(exp);
return (mapping.findForward( "ERROR "));
}
return (mapping.findForward(KH010Constants.PAGE_KH016));
}
private boolean copy2 (FormFile ff,String fullPath)throws Exception
{
String fname=ff.getFileName();
InputStream in=ff.getInputStream();
OutputStream out=new FileOutputStream(fullPath+fname);// "/ "表示保存的路径可以修改为 "load/ "
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=in.read(buffer,0,8192))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
in.close();
ff.destroy();
return true;
}