上传到servlet

时间:2014-01-23 05:53:06

标签: java servlets

我正在尝试使用servlet上传图片并为其添加标题/标题,然后将图像存储在文件夹中,获取图像路径然后将其存储到数据库中。

如何管理文本框中的值?我有一个servlet将上传图像,但如果我把输入框我得到空指针。我是否将其置于另一种形式并进行另一个过程?

<form method="post" action="gallery/upload" enctype="multipart/form-data">
    <input type="text" class="form-control" required="" placeholder="Caption" id="newsboxTitle" name="title">
    Select file to upload:
    <input type="file" name="dataFile" accept="image/*" id="fileChooser" class="btn btn-default"/><br/><br/>
    <input type="submit" value="Upload" class="btn btn-primary" /> 
</form>

如果我删除文本框,则会将图像存储在文件夹中。谢谢! :)

编辑: 嗯,刚刚发现它与这个名字有关。 Works但文本字段没有名称......

1 个答案:

答案 0 :(得分:0)

我在JSP中做过这个。检查它是否对您有所帮助。 这是第一个JSP页面。它包含您可以为图像指定路径的表单。

<form  ENCTYPE="multipart/form-data" ACTION="redirectpicupload.jsp" METHOD=POST>
   <kbd><br>
 </kbd>
            <center>
              <p align="center"><center> 
                <kbd><span class="style18"> Upload Image File 
                                </span>
                            </kbd>
                </center></p>
                   <kbd><br />
                    <b>Choose the file To Upload:</b>
                   <INPUT NAME="F1" TYPE="file" class="style17">
                                    </kbd>
              <p align="center"><kbd>
                             <select name="type" id="type" class="style14">
                                 <option>Profile</option>
                                   <option>    Wall </option>
                                    <option>   Casual </option>
                             </select>    <br></br>
                <INPUT TYPE="submit" class="style17" VALUE="Send File" >
                </kbd></p>

 </center>
          </form>

现在它被重定向到redirectpicupload.jsp

//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
 DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
//extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
File f=new File(saveFile);
f.renameTo(new File   ("C:/Users/ACER/Documents/NetBeansProjects/buddynet/web/images/"+saveFile)); 
// The path where to place the file
Connection con;
ResultSet rs;
Statement stmt;
String qry , uid , imgfor;
imgfor=null;
imgfor=request.getParameter("type");
out.println(imgfor);
String ff="images/" + saveFile;
uid=session.getAttribute("uid").toString();
qry = "insert into images(username,image_path)values('"+ uid +"','"+ ff +"')";

这里“ff”包含文件路径。如果您对上述代码有任何疑问,请告诉我。