3

我需要在服务器端的特定目录中写入图像数据,但是我尝试从 html 表单和 jquery ajaxuploader 插件从此处发送的原始字节 [] 图像上传数据为空。

以下是我用来处理上传图像的原始字节的控制器的片段:

@RequestMapping(value = "uploadImage", method = RequestMethod.POST)
public void uploadImage(byte[] uploadData, Writer writer, HttpServletRequest request) throws IOException, JSONException {
    //uploadData is turning out to be null
    //..
}

@InitBinder
protected void initBinder(ServletRequestDataBinder binder) {
    binder.registerCustomEditor(byte[].class,
            new ByteArrayMultipartFileEditor());
}

我在 spring 配置文件中配置了以下用于处理上传的内容:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

我正在使用 Spring MVC 3。有人可以指导我如何发送上传数据的原始字节吗?

谢谢。

4

2 回答 2

2

First, if you're form is uploading an image, make sure your content type is "multipart/form-data". You might want to change your RequestMapping as follows:

@RequestMapping(value = "uploadImage", method = RequestMethod.POST, headers={"content-type=multipart/form-data"})

Also, I'd suggest using CommonsMultipartFile to handle the upload. Change your function signature as follows, where "fieldName" is the name of the input field in your form:

public void uploadImage(byte[] uploadData, Writer writer, HttpServletRequest request, @RequestParam("fieldName") CommonsMultipartFile file)

Then you can get the raw bytes as follows:

file.getBytes()

Make sure you include the commons-fileupload dependency for CommonsMultipartFile.

I'm using spring3 + jquery ajaxform and this works like a charm. Hope this helps!

于 2012-06-05T01:06:18.773 回答
2

以下是我在客户端使用的 JavaScript 和 HTML 代码,它们可以正常工作:

JavaScript

function createUploader(){           
    var uploader = new qq.FileUploader({
    element: document.getElementById('file-uploader'),
    action: 'uploadImage',
    allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
    debug: true,
    onSubmit: function(id, fileName){
        console.log("id - " + id + ", fileName - " + fileName);
    },
        onComplete: function(id, fileName, responseJSON) {
            console.log("responseJSON -  " + responseJSON);
        }
    });           
}
window.onload = createUploader; 

HTML

<div id="file-uploader" >
    <noscript>
    <p>Please enable JavaScript to upload your property location images</p>
    </noscript>
</div>

控制器

@Controller
public class FranchiseeLocationImageController {

    private static final Log logger = LogFactory.getLog(FranchiseeLocationImageController.class);

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private FranchiseeLocationImageService franchiseeLocationImageService;  

    @RequestMapping(value = "uploadImage", method = RequestMethod.POST)
    public void uploadImage(byte[] qqfile, Writer writer, @RequestParam("qqfile") String img, HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException{
        FranchiseeLocationImage image = null;       
        PrintWriter pr = null;
        InputStream is =  null;
        File file = null;
        FileOutputStream fos = null;
        String filename = request.getHeader("X-File-Name");
        String imageId = FilenameUtils.removeExtension(img);
        String imageFormat = franchiseeLocationImageService.getImageFormat();
        String outputDir = servletContext.getRealPath("") + File.separator + franchiseeLocationImageService.getImagesDirectory() + File.separator;
        File baseDirectory = null;
        File output = null;
        String path = FilenameUtils.removeExtension(img) + "." + imageFormat;
        File outputDirectory = null;
        HttpSession session = request.getSession();
        /*
        HttpSession session = request.getSession(false);
        if(session == null) {
            session = request.getSession();
        }
        */
        List<String> franchiseeLocationImages = (List<String>) session.getAttribute("franchiseeLocationImages");
        if(franchiseeLocationImages == null) {
            franchiseeLocationImages = new ArrayList<String>();
        }

        logger.debug( "filename - " + filename + " | img - " + img + " | img name - " + FilenameUtils.removeExtension(img) + " | img format - " + FilenameUtils.getExtension(img) + " | uploadData - " + qqfile + " | imageFormat - " + imageFormat);   
        /**
         * Reading the image being uploaded and writing it to images/franchiseeLocation folder ["qqfile" is used instead of "X-File-Name" as "X-File-Name" gives encoded HTML name with "%20" for " "]
         */
        try {
            pr = response.getWriter();
            is =  request.getInputStream();

            /*
            baseDirectory = new File(outputDir);
            baseDirectory.mkdirs();
            file = new File(outputDir, FilenameUtils.removeExtension(img) + "." + imageFormat);         
            fos = new FileOutputStream(file);
            int copiedNum = IOUtils.copy(is, fos);
            */

            outputDirectory = new File(outputDir);
            outputDirectory.mkdirs();
            output = new File(outputDirectory, path);
            BufferedImage sourceImage = ImageIO.read(is);
            boolean written = ImageIO.write(sourceImage, imageFormat, output);
            franchiseeLocationImages.add(img);
            session.setAttribute("franchiseeLocationImages", franchiseeLocationImages);

            logger.debug("franchiseeLocationImages - " + franchiseeLocationImages);
            logger.debug("outputDirectory - " + outputDirectory + " | output - " + output +  " | sourceImage - " + sourceImage + " | is - " + is + " | file - " + file + " |fos - " + fos + " | copiedNum - " + "copiedNum" + " | baseDirectory - " + baseDirectory + " | sourceImage - " + sourceImage + " | written - " + written);
            /*
            image = franchiseeLocationImageService.processProductImage(qqfile, imageId);
            JSONObject json = new JSONObject();
            json.put("path", image.getPath());
            json.put("id", image.getId());
            writer.write(json.toString());
            */
            pr.print("{success: true}");
        } finally {
            writer.close();
            /*
            try {
                fos.close();
                is.close();
            } catch (IOException ignored) {
            }
            */
            pr.flush();
            pr.close();
        }
    }

    @InitBinder
    protected void initBinder(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());
    }

    private static String html2text(String html) {
        return Jsoup.parse(html).text();
    }


}
于 2012-10-20T11:48:25.377 回答