extjs3+springMVC上传文件

本文介绍如何使用ExtJS实现文件上传功能,并详细解释了SpringMVC控制器如何接收并处理上传的文件,包括文件名修改及FTP上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Ext代码

//formPanel表单的属性加入
fileUpload: true,  //上传文件
//上传文件表单
 {
	fieldLabel : '上传文件',
	name : 'uploadphoto',
	inputType : 'file'
}

 2.springMVC controller

@RequestMapping(params = "method=insert")
	public ModelAndView insert(Student stu,HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		int count = dataZxZdbnrService.insertStudent(stu);
		reponse.setContentType("text/html");   //必须,否则会抛异常
		String result = "";
		if (count > 0) {
			count = studentService.findByLast();
			uploadTemplate(request,count);
			result = "{stuid:"+count+",success:true}";
		}
		reponse.getWriter().write(result);
		return null;
	}
	
	public void uploadTemplate(HttpServletRequest request,int id) throws Exception{
		   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
		   MultipartFile file = multipartRequest.getFile("uploadphoto"); 
		   if(file.getOriginalFilename()!=null&&!file.getOriginalFilename().equals("")){
			   String filenameold = file.getOriginalFilename(); 
			   String hz = filenameold.substring(filenameold.lastIndexOf(".", filenameold.length()), filenameold.length());
			   String filename =String.valueOf(id); 
			   String newfilename=filename+hz; 
			   long filesize=file.getSize();
			   String[] strFilePath = new String[]{ "template"};
			   InputStream input = file.getInputStream();  
			   this.upload(strFilePath, filesize, newfilename, input);
		   }
	}
	
	public boolean upload(String[] strFilePath, long fileSize,
			String strNewFileName,InputStream is) throws Exception {
		boolean bUpload = false;
		FtpBean ftp = new FtpBean(); 
		try {
			ftp.setSocketTimeout(12500);
			ftp.setPassiveModeTransfer(false);
			ftp.setPort(20);
			ftp.ftpConnect("192.168.1.33","zhou","zhou");
		} catch (Exception e) {
			if (is != null) {
				is.close();
			}
			ftp.close();
			throw e;
		}
		for (int i = 0; i < strFilePath.length; i++) {// 鐢熸垚鎴栬缃洰褰�
			try {
				ftp.makeDirectory(strFilePath[i]);
			} catch (Exception e) {

			} finally {
				ftp.setDirectory(strFilePath[i]);
			}
		}
		try {
			byte[] bytes = getBytesFromStream(is, (int) fileSize);
			ftp.putBinaryFile(strNewFileName, bytes);
			bUpload = true;
		} catch (Exception e) {
			throw e;
		} finally {
			if (is != null) {
				is.close();
			}
			ftp.close();
		}
		return bUpload;

	}

	public byte[] getBytesFromStream(InputStream is, int StreamSize)
			throws Exception {
		byte[] bytes = new byte[StreamSize];
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}
		if (offset < bytes.length) {
			throw new Exception("鏂囦欢娴侀敊璇� ");
		}
		is.close();
		return bytes;
	}

 3.applicationContext.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
</bean>

 4.加入必要jar包:

    (1)commons-fileupload-1.2.jar

    (2)commons-io-1.3.1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值