[英]Is it posible to change upload path with a servlet?
我將嘗試解釋我想在這里做什么以及為什么我需要這樣做。
我知道我可以使用像我在附近的servlet一樣將我的上下文路徑內部重定向到上下文路徑之外,這可以很好地工作:
@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {
// Properties ---------------------------------------------------------------------------------
private String imagePath;
// Init ---------------------------------------------------------------------------------------
public void init() throws ServletException {
// Define base path somehow. You can define it as init-param of the servlet.
this.imagePath = "/home/mycomp/images/";
// In a Windows environment with the Applicationserver running on the
// c: volume, the above path is exactly the same as "c:\var\webapp\images".
// In Linux/Mac/UNIX, it is just straightforward "/var/webapp/images".
}
// Actions ------------------------------------------------------------------------------------
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get requested image by path info.
String requestedImage = request.getPathInfo();
// Check if file name is actually supplied to the request URI.
if (requestedImage == null) {
// Do your thing if the image is not supplied to the request URI.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Decode the file name (might contain spaces and on) and prepare file object.
File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
// Check if file actually exists in filesystem.
if (!image.exists()) {
// Do your thing if the file appears to be non-existing.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Get content type by filename.
String contentType = getServletContext().getMimeType(image.getName());
// Check if file is actually an image (avoid download of other files by hackers!).
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
if (contentType == null || !contentType.startsWith("image")) {
// Do your thing if the file appears not being a real image.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
// Write image content to response.
Files.copy(image.toPath(), response.getOutputStream());
}
}
因此,每當我在上下文“/webApp/images/mypic.jpg”中使用路徑時,它實際上從我的上下文圖像文件夾“/ home / mycomp / images /”中獲取圖像。
它很棒,因為我不能修改原始代碼,我只需要添加這個servlet來將其重定向到上下文之外。 這就是我試圖做同樣但上傳的原因。 我得到了下一段代碼來獲取保存圖像的路徑並將其保存,目前在上下文中:
String file = "imagen" + (1 + (int) (Math.random() * 100000));
String path = httpServletRequest.getSession().getServletContext().getRealPath("/images/");
InputStream in = imagen.getInputStream();
OutputStream bos = new FileOutputStream(path + "/" + file);
int byteRead = 0;
byte[] buffer = new byte[8192];
while ((byteRead = in.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, byteRead);
}
bos.close();
in.close();
(我知道我錯過了實際獲取圖像的部分,但我不需要顯示它。)這會將圖像保存在“/ home / mycomp / project / webApp / images /”中,因為這是getRealPath,它顯然在上下文中。
編輯:這是在一個struts1動作中完成的,這會使事情變得復雜一些,因為現在我已經明白我不能從servlet中調用一個動作,或者我可以嗎?
這引出了我們的問題,是否可以將其保存在上下文之外而不修改使用servlet之類的東西保存圖像的代碼,這樣就可以將其保存到“/ home / mycomp / project / webApp / images /” ,它會將其保存到“/ home / mycomp / images /”?
當然,您可以更改上傳目錄。
使用注釋:
@MultipartConfig(
location="/tmp", // <-----
fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5,
maxRequestSize=1024*1024*5*5
)
或使用web.xml
<multipart-config>
<location>/tmp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
參考: https : //docs.oracle.com/javaee/7/tutorial/servlets011.htm
答案是否定的,不是我想做的方式,現在我更了解servlet是什么以及它們是如何工作的。
我試圖做的是用strlet覆蓋struts動作中的上傳路徑,這是無法完成的,因為servlet將被執行INSTEAD動作。 所以不,你不能使用servlet來覆蓋動作中的上傳路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.