---
title: "java调用一体机扫描关键核心代码"
author: "peihexian"
date: "2017-11-04T21:20:46Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2018-01-17T16:02:44Z"
id: 2398674
url: "https://peihexian.iteye.com/blog/2398674"
---

springmvc环境下调用一体机实现图片扫描

下面是驱动扫描一体机打印机去进行图像扫描的代码。  
\[code=&quot;html&quot;]

function scan() {  
document.getElementById('scaner1').scan();  
}

function upload() {  
var status = document.getElementById('scaner1').uploadAllAsTIFFormatToServer('&lt;%=app\_url%&gt;/upload/uploadimg', '123456','');  
if (null==status){  
alert(&quot;上传失败!可能是安全框架拦截了上传请求!&quot;);  
return ;  
}  
alert(status);  
var json\_obj=eval(&quot;(&quot;+status+&quot;)&quot;);  
if (true==json\_obj.status){  
alert(&quot;上传成功!&quot;);  
}else{  
alert(&quot;上传失败!&quot;);  
}  
}  
function upload\_pdf() {  
var status = document.getElementById('scaner1').uploadAllAsPDFFormatToServer('&lt;%=app\_url%&gt;/upload/uploadimg', '123456','');  
if (null==status){  
alert(&quot;上传失败!可能是安全框架拦截了上传请求!&quot;);  
return ;  
}  
alert(status);  
var json\_obj=eval(&quot;(&quot;+status+&quot;)&quot;);  
if (true==json\_obj.status){  
alert(&quot;上传成功!&quot;);  
}else{  
alert(&quot;上传失败!&quot;);  
}  
}

     
 

这个是后台接收一体机扫描结果数据的服务器端mvc代码  
\[code=&quot;java&quot;]  
package cn.brainysoft.web;

import java.io.File;  
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.multipart.MultipartFile;

@Controller  
@RequestMapping(&quot;/upload&quot;)  
public class ImageUploader {

@RequestMapping(value = &quot;/uploadimg&quot;)  
@ResponseBody  
public String uploadimg(HttpServletRequest request, @RequestParam(&quot;imageCount&quot;) Integer imageCount, @RequestParam(&quot;id&quot;) String id,  
@RequestParam(&quot;desc&quot;) String desc,@RequestParam(&quot;image&quot;) MultipartFile myfile) {  
System.out.println(&quot;收到客户端请求....&quot;);  
if (null != myfile) {  
if (myfile.isEmpty()) {  
System.out.println(&quot;文件未上传&quot;);  
} else {  
System.out.println(&quot;文件长度: &quot; + myfile.getSize());  
System.out.println(&quot;文件类型: &quot; + myfile.getContentType());  
System.out.println(&quot;文件名称: &quot; + myfile.getName());  
System.out.println(&quot;文件原名: &quot; + myfile.getOriginalFilename());  
System.out.println(&quot;========================================&quot;);  
// 如果用的是Tomcat服务器，则文件会上传到\\\\%TOMCAT\_HOME%\\\\webapps\\\\YourWebProject\\\\img\\\\upload\\\\文件夹中  
String realPath = request.getServletContext().getRealPath(&quot;/img/upload&quot;);  
// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉  
try {  
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));  
System.out.println(&quot;上传成功,请打开&quot;+realPath+&quot;查看上传结果&quot;);  
return &quot;{status:true}&quot;;  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  
return &quot;{status:false}&quot;;  
}

}

 貌似代码编辑器乱了，完整示例代码见附件吧。