`

extjs3+springMVC上传文件

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics