sll796532 2014-08-01 09:15 采纳率: 0%
浏览 1842

uploadify上传时文件传进去了但是报302错误

我项目用的ssi框架,uploadify版本用的2.1.0。网上说是要在上传地址要改为这样----》'script':'uploadImage.htm;jsessionid='+'<%=request.getSession().getId()%>',但是我这还是报302错误。求解答。。。。 help......

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 10:09
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    如果你使用的是Java 8或者更高版本的JSP,你可以尝试以下方法来解决你的问题:

    首先,在你的uploadImage.jsp中添加如下代码:

    <script type="text/javascript">
        function uploadImage() {
            var file = document.getElementById('file').files[0];
            var formData = new FormData();
            formData.append("image", file);
            $.ajax({
                url: 'your_upload_url',
                data: formData,
                contentType: false,
                processData: false,
                type: 'POST',
                success: function(response) {
                    alert("File uploaded successfully");
                },
                error: function(xhr, status, err) {
                    console.log(err);
                    alert("Error uploading file");
                }
            });
        }
    </script>
    

    然后,在你的uploadImage.htm中加入以下代码:

    <%@ page import="javax.servlet.http.*" %>
    
    <%
    response.setContentType("text/html;charset=UTF-8");
    
    String imageFile = request.getParameter("image");
    if (imageFile != null && !"".equals(imageFile)) {
        File f = new File(request.getSession().getId() + "_" + imageFile);
        try {
            f.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    
        String uploadUrl = "http://your-server/upload";
        if (!uploadUrl.endsWith("/")) {
            uploadUrl += "/";
        }
        // 在这里替换为你实际的上传URL
        uploadUrl += request.getSession().getId() + "_" + imageFile;
    
        URLConnection conn = new URL(uploadUrl).openConnection();
        OutputStream os = conn.getOutputStream();
    
        InputStream is = new FileInputStream(f);
    
        byte[] buffer = new byte[4096];
    
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    
        os.close();
        is.close();
    
        response.sendRedirect(uploadUrl);
    }
    %>
    

    这个解决方案应该能帮助你解决问题。

    注意:你需要将uploadUrl变量中的路径和参数替换为你的实际情况。

    评论

报告相同问题?