apache常用库文件上传

发布于 2024-12-21 10:48:28 字数 1467 浏览 0 评论 0原文

public void execute(HttpServletRequest request) throws Exception {
    DiskFileItemFactory factory = new DiskFileItemFactory();

    factory.setSizeThreshold(1*1024*1024*1024); //1 MB
    /*
     * Set the temporary directory to store the uploaded files of size above threshold.
     */
    factory.setRepository(new File("c:\\temp"));

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List items = upload.parseRequest(request);
    Iterator iter = items.iterator();

    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
            InputStream uploadedStream = item.getInputStream();
            try {
                File f = new File("C:\\temp\\index.jpg");
                item.write(f);
                uploadedStream.close();
            } 
            catch (IOException e) {
            }
        }
    }

html表单:

<form enctype="multipart/form-data" method="POST" action="<%=request.getContextPath ()%>/main?cmd=ci">
    <table class = "lineable">
        <tr>
            <td><input type="file" name="file1"/></td>
            <td><input type="submit" name="q" value="import"/></td>
        </tr>
    </table>
</form>

当我保存在其中时,创建index.jpg,但在jpg文件中写入“导入”字样,其中提交按钮的值。怎么了。谢谢。

public void execute(HttpServletRequest request) throws Exception {
    DiskFileItemFactory factory = new DiskFileItemFactory();

    factory.setSizeThreshold(1*1024*1024*1024); //1 MB
    /*
     * Set the temporary directory to store the uploaded files of size above threshold.
     */
    factory.setRepository(new File("c:\\temp"));

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List items = upload.parseRequest(request);
    Iterator iter = items.iterator();

    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
            InputStream uploadedStream = item.getInputStream();
            try {
                File f = new File("C:\\temp\\index.jpg");
                item.write(f);
                uploadedStream.close();
            } 
            catch (IOException e) {
            }
        }
    }

the html form:

<form enctype="multipart/form-data" method="POST" action="<%=request.getContextPath ()%>/main?cmd=ci">
    <table class = "lineable">
        <tr>
            <td><input type="file" name="file1"/></td>
            <td><input type="submit" name="q" value="import"/></td>
        </tr>
    </table>
</form>

When i save in it create index.jpg but writes "import" word in the jpg file which value of submit button. What is wrong. thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-12-28 10:48:28

这里至少有一个问题:

if(item.isFormField()){
    InputStream uploadedStream = item.getInputStream();
    ...
}

如果您正在查看表单字段,为什么要查看它的InputStream?您应该只对非表单字段项目(即文件)感兴趣。

经过第二次检查,您正在使用 Commons IO,甚至不需要查看它的 InputStream。只要取消支票就可以了。

At least one problem here:

if(item.isFormField()){
    InputStream uploadedStream = item.getInputStream();
    ...
}

If you're looking at a form field, why should you look at its InputStream? You should only be interested in non form field items, ie files.

Upon second inspection, you're using the Commons IO, and you don't even need to look at its InputStream. Just negate the check and you'll be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文