上传一个文件到服务器,处理它,创建一个新文件(txt文件)并提示保存
Upload a file to server, process it, create a new file (txt file) and prompt for save
好的,到目前为止我知道如何将文件上传到服务器并处理它(post 方法)。我也知道如何从服务器导出文件(获取方法),但我不知道如何在同一个 servlet/action 中执行此操作。我的意思是上传一个文件,处理它,创建一个 txt(或其他类型的文件),然后提示用户保存新创建的文件。任何帮助都会很棒。谢谢
听起来你已经拥有了你所需要的一切,你只需要将上传和下载结合起来。
@RemoteServiceRelativePath("MyServlet")
public class MyServlet extends HttpServlet {
@Override
protected final void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String fileContent = "";
try {
List<FileItem> items = upload.parseRequest(req);
//the items.get(0) is very error-prone, but good enough for this example
fileContent = IOUtils.toString(items.get(0).getInputStream(), Charset.forName("UTF-8"));
} catch (FileUploadException e) {
e.printStackTrace();
}
String fileName = "filename.txt";
resp.setContentType("text/plain;charset=UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream out = resp.getOutputStream();
out.write(fileContent.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
}
}
在我的示例中,我使用 Apache Commons FileUpload 库读取上传的文件,但您提到您已经知道如何存档它。但我将其包括在内是为了提供一个带有上传和下载的完整示例。自己也偷懒用IOUtils把上传File的InputStream
转成String
这个非常基本的示例读取上传文件的内容并将其作为下载提供给客户端。因此我们写入响应的 OutputStream
并设置相关的 headers 和响应的内容类型。
其他需要了解的事情
如果可能,请避免使用您的处理结果(在服务器上)创建文件,因为那样您必须在下载后删除它,这非常棘手。
此外,此示例不包含任何身份验证,并且具有非常(!)基本的错误处理。您应该始终检查请求是否有效,如果没有设置正确的 (resp.sendError()
) http 状态代码 (401,400,...)。如果你处理失败,你也应该设置相应的状态码。
好的,到目前为止我知道如何将文件上传到服务器并处理它(post 方法)。我也知道如何从服务器导出文件(获取方法),但我不知道如何在同一个 servlet/action 中执行此操作。我的意思是上传一个文件,处理它,创建一个 txt(或其他类型的文件),然后提示用户保存新创建的文件。任何帮助都会很棒。谢谢
听起来你已经拥有了你所需要的一切,你只需要将上传和下载结合起来。
@RemoteServiceRelativePath("MyServlet")
public class MyServlet extends HttpServlet {
@Override
protected final void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String fileContent = "";
try {
List<FileItem> items = upload.parseRequest(req);
//the items.get(0) is very error-prone, but good enough for this example
fileContent = IOUtils.toString(items.get(0).getInputStream(), Charset.forName("UTF-8"));
} catch (FileUploadException e) {
e.printStackTrace();
}
String fileName = "filename.txt";
resp.setContentType("text/plain;charset=UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream out = resp.getOutputStream();
out.write(fileContent.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
}
}
在我的示例中,我使用 Apache Commons FileUpload 库读取上传的文件,但您提到您已经知道如何存档它。但我将其包括在内是为了提供一个带有上传和下载的完整示例。自己也偷懒用IOUtils把上传File的InputStream
转成String
这个非常基本的示例读取上传文件的内容并将其作为下载提供给客户端。因此我们写入响应的 OutputStream
并设置相关的 headers 和响应的内容类型。
其他需要了解的事情
如果可能,请避免使用您的处理结果(在服务器上)创建文件,因为那样您必须在下载后删除它,这非常棘手。
此外,此示例不包含任何身份验证,并且具有非常(!)基本的错误处理。您应该始终检查请求是否有效,如果没有设置正确的 (resp.sendError()
) http 状态代码 (401,400,...)。如果你处理失败,你也应该设置相应的状态码。