如何使任何人都可以访问JSP上载的文件?

时间:2020-04-10 15:38:46

标签: jsp servlets localhost

我正在创建一个基于JSP / servlet的Web应用程序,该文件将文件上传到服务器上的文件夹。
但是,当我点击上载文件的链接时,我将无法访问它。
例如。我已将文件上传到/Projects/MyFiles/file.html
当我尝试使用访问该文件时 localhost:8083/Projects/MyFiles/file.html无效。

有人可以帮忙吗?

这是我的代码

@WebServlet("/uploadIDCM")
public class Servlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String finalString = "Yet to";
        if(ServletFileUpload.isMultipartContent(request)){
            try {
                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                        String name = new File(item.getName()).getName();
                        item.write( new File("/Projects/MyFiles/" + File.separator + name));
                    }
                }
                finalString = "Success";
                request.setAttribute("msg", "File Uploaded Successfully");
            } catch (Exception ex) {
                finalString = "Exception" + ex.toString();
                request.setAttribute("msg", "File Upload Failed due to " + ex);
            }
        }else{
            finalString = "No file found";
            request.setAttribute("msg","No File found");
        }

    }
}

index.js文件是

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>IDCM Builds</title>
</head>
<body>
    <Center>
        <P>Upload the iOS or Android file(s) here. Upload one by one, or all at once of a specific type</P>
        <P>For .html and plist files, destination folder is determined by the File Type option you use below</P>
        <P><B>Note:</B> .html, .plist, .ipa or .apk extensions only accepted as valid files</P>
            <form action="uploadIDCM" method="post" enctype="multipart/form-data">
                <table border=1><tr><Td><label for="type">File(s) Type :</label></td>
                    <td><select name="ftype">
                        <option value="iOS_Build">iOS_Build</option>
                        <option value="Android_Build">Android_Build</option>
                    </select></td></tr>
                    <tr><Td><label for="file">Upload File(s):</label></Td>
                        <Td><input type="file" name="file[]" multiple ></Td></tr>
                </table>
        <input name="submit" type="submit" value="Upload" />
    </Center>
        </form>
</body>
</html>

0 个答案:

没有答案
相关问题