java springMVC 上传文件

所需功能,浏览器上传本地文件,服务器上传到ftp.

直接js 传文件路径到后台,后台根据文件路径上传ftp会有问题,其他电脑访问服务器操作上传功能会出现上传失败的。因为服务器是获取本地文件路径的,其他电脑文所在的路,本地文件下是没有的。
需要先把文件下载到服务器的临时文件下,然后再从服务器上传文件到ftp.上传后删除临时文件夹。具体代码:
html:
<form id="fm" name= "mfm" method = "post" style="display:inline;" 
			action = "/jzkz-web/ZCPage/uploadFile" enctype="multipart/form-data"
			οnsubmit="return backUpload();">
		<input type ='file' style="display:none" name='file' id='file'></file>
		<input type = "submit" class="btn btn-success btn-mini" value = "上传"/>
	</form>



js:
function backUpload(){
		//弹出文件选择框
		document.getElementById("file").click();
		// jquery 表单提交 
		$("#fm").ajaxSubmit(function(message) { 
		// 对于表单提交成功后处理,message为提交页面saveReport.htm的返回内容 
		}); 
		return false;
	}



java
/**
	 * 上传文件
	 * @param request
	 * @param response
	 * @throws IOException 
	 * 
	 * 通过流的方式上传文件
	 * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
	 */
    @RequestMapping("uploadFile")
	@ResponseBody
    public void  fileUpload(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file) throws IOException {
		int ftpPort = 3121;
		String ftpName = "root";
		String ftpPwd = "hisome";
		String ftpUrl = "//home";
    	//判断上传ftp是否成功
		boolean result=false;
		//判断上传到服务器是否成功
		boolean upToServlet=false;
		String uploadPath = null;
		File createFile = null;
        try {
            uploadPath =  request.getSession().getServletContext().getRealPath("./") + File.separator + "upFile";
            System.out.println("====需要删除的文件夹====="+uploadPath);
            //新建文件夹
            createFile = new File(uploadPath);
            if (createFile.exists()) {// 判断文件是否存在
    			System.out.println("目标文件已存在");
    		}else{
    			createFile.mkdirs();
    		}
            
            System.out.println(uploadPath+"/"+ file.getOriginalFilename());
            //获取输出流
            OutputStream os=new FileOutputStream(uploadPath +"/"+ file.getOriginalFilename());
            //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
            InputStream is=file.getInputStream();
            int temp;
            //一个一个字节的读取并写入
            while((temp=is.read())!=(-1))
            {
                os.write(temp);
            }
           os.flush();
           os.close();
           is.close();
           upToServlet=true;
         
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
        	upToServlet=false;
            e.printStackTrace();
        }
        System.out.println("上传服务器成功!");
        if(upToServlet){
        	System.out.println(ZCIP);
        	FTPClient ftp = new FTPClient();   
			boolean connectRes=false;
			int reply;
			try{
				ftp.connect(ZCIP,ftpPort);      
				ftp.login(ftpName,ftpPwd);      
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);      
				reply = ftp.getReplyCode();      
				if (!FTPReply.isPositiveCompletion(reply)) {      
					ftp.disconnect();  
				}else{
					ftp.changeWorkingDirectory(ftpUrl);
					System.out.println(ftpUrl);
					connectRes = true;  
				}
			}catch(Exception e){
				
			}
			System.out.println("connectRes      "+connectRes);
			if(connectRes){
				try{
					File newfile = new File(uploadPath+"\\"+file.getOriginalFilename());
					upload(newfile,ftp);
					result=true;
				}catch(Exception e){
					System.out.println("upload error");
					result=false;
				}
			}
        }
        //删除文件夹 这个类可以参考我的 java 删除文件 目录 文件夹
        DeleteFileUtil.deleteDirectory(uploadPath);
        return ;
    }


喜欢点赞!!!!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值