gpt4 book ai didi

java - 以非阻塞方式处理文件上传

转载 作者:行者123 更新时间:2023-12-02 07:32:59 25 4
gpt4 key购买 nike

后台线程是here

为了明确目标 - 用户将上传一个大文件,并且必须立即重定向到另一个页面以进行不同的操作。但文件很大,从 Controller 的InputStream读取需要时间。所以我不情愿地决定fork一个新的Thread来处理这个I/O。代码如下:

Controller servlet

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

System.out.println("In Controller.doPost(...)");

TempModel tempModel = new TempModel();
tempModel.uploadSegYFile(request, response);

System.out.println("Forwarding to Accepted.jsp");

/*try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/

request.getRequestDispatcher("/jsp/Accepted.jsp").forward(request,
response);
}

模型类

package com.model;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utils.ProcessUtils;

public class TempModel {

public void uploadSegYFile(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub

System.out.println("In TempModel.uploadSegYFile(...)");

/*
* Trigger the upload/processing code in a thread, return immediately
* and notify when the thread completes
*/
try {
FileUploaderRunnable fileUploadRunnable = new FileUploaderRunnable(
request.getInputStream());

/*
* Future<FileUploaderRunnable> future = ProcessUtils.submitTask(
* fileUploadRunnable, fileUploadRunnable);
*
* FileUploaderRunnable processed = future.get();
*
* System.out.println("Is file uploaded : " +
* processed.isFileUploaded());
*/

Thread uploadThread = new Thread(fileUploadRunnable);
uploadThread.start();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*
* catch (InterruptedException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } catch (ExecutionException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*/

System.out.println("Returning from TempModel.uploadSegYFile(...)");
}

}

可运行

package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class FileUploaderRunnable implements Runnable {

private boolean isFileUploaded = false;
private InputStream inputStream = null;

public FileUploaderRunnable(InputStream inputStream) {
// TODO Auto-generated constructor stub
this.inputStream = inputStream;
}

public void run() {
// TODO Auto-generated method stub

/* Read from InputStream. If success, set isFileUploaded = true */
System.out.println("Starting upload in a thread");

File outputFile = new File("D:/06c01_output.seg");/*
* This will be changed
* later
*/
FileOutputStream fos;
ReadableByteChannel readable = Channels.newChannel(inputStream);
ByteBuffer buffer = ByteBuffer.allocate(1000000);

try {

fos = new FileOutputStream(outputFile);

while (readable.read(buffer) != -1) {
fos.write(buffer.array());
buffer.clear();
}

fos.flush();
fos.close();

readable.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("File upload thread completed");
}

public boolean isFileUploaded() {
return isFileUploaded;
}

}

我的疑问/疑问:

  1. 从 Servlet 手动生成线程在逻辑上对我来说是有意义的,但在编码方面却让我害怕 - 容器毕竟不知道这些线程(我认为是这样!)
  2. 当前代码给出了一个非常明显的异常 - 由于 doPost(...) 方法在 run() 方法完成之前返回,因此无法访问流:

    In Controller.doPost(...)
    In TempModel.uploadSegYFile(...)
    Returning from TempModel.uploadSegYFile(...)
    Forwarding to Accepted.jsp
    Starting upload in a thread
    Exception in thread "Thread-4" java.lang.NullPointerException
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:512)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:497)
    at org.apache.coyote.http11.InternalInputBuffer$InputStreamInputBuffer.doRead(InternalInputBuffer.java:559)
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:324)
    at org.apache.coyote.Request.doRead(Request.java:422)
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:287)
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:407)
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:310)
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:202)
    at java.nio.channels.Channels$ReadableByteChannelImpl.read(Unknown Source)
    at com.model.FileUploaderRunnable.run(FileUploaderRunnable.java:39)
    at java.lang.Thread.run(Unknown Source)
  3. 记住第一点,使用 Executor 框架对我有帮助吗?

    package com.utils;

    import java.util.concurrent.Future;
    import java.util.concurrent.ScheduledThreadPoolExecutor;

    public final class ProcessUtils {

    /* Ensure that no more than 2 uploads,processing req. are allowed */
    private static final ScheduledThreadPoolExecutor threadPoolExec = new ScheduledThreadPoolExecutor(
    2);

    public static <T> Future<T> submitTask(Runnable task, T result) {

    return threadPoolExec.submit(task, result);
    }
    }

那么我应该如何确保用户不会阻塞并且流仍然可访问,以便可以从中读取(上传的)文件

最佳答案

实际上并非如此。您正在尝试生成线程并读取 POST 请求的内容,并且您还尝试将用户转发到具有相同请求对象的另一个页面。这会让 servlet 容器感到困惑。

你可以

  • 使用单独的框架来上传表单和 Controller
  • 使用带有上传表单和单独 Controller 的弹出窗口
  • 使用 AJAX 就地加载下一页,同时仍然上传当前页面中的内容(让浏览器为您处理)

关于java - 以非阻塞方式处理文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com