java - 如何同时从表单中获取图像名称和另一个输入字段值到 servlet

标签 java javascript mysql jsp servlets

<分区>

我正在使用 jsp 和 servlet 上传图像。我正在使用 enctype=multipart/form-data。但是我无法将输入字段值提取到 servlet 中。

这是我的jsp代码

<form method="post" action="uploadImage" enctype="multipart/form-data">
        <p>Select file to upload:</p>
        <input type="file" name="file" >
        <input type="text" name="name" >
        <br><br>
        <input type="submit" value="Upload" >
    </form>

这是我的sevlet代码

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
           response.setContentType("text/html;charset=UTF-8");
    HttpSession sess=request.getSession();
    PrintWriter out = response.getWriter();
int i=0;
    String u_id=(String) sess.getAttribute("uid");
    String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}

saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
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;

File ff = new File("D:/images/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
try{
Connection con;
PreparedStatement ps,ps1;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dfmc","root","93Pb3gaNv0");
String sql="insert into profilepic_table(uid, image_name) values('"+u_id+"','"+ff.getName()+"')";
ps=con.prepareStatement(sql);
ps.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}
}

另外我想选择多个文件并上传。

最佳答案

在定义 Servlet 之前添加以下代码行,以处理必须使用 Multipart 注释的 multipart/form-data 提交

@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
    maxFileSize = 1024 * 1024 * 10, // 10MB
    maxRequestSize = 1024 * 1024 * 50)

public class ServletName extends HttpServlet { ... }

此外,导入 javax.servlet.annotation.MultipartConfig;

关于java - 如何同时从表单中获取图像名称和另一个输入字段值到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29619906/

相关文章:

php - 通过ajax在html/php中搜索表单

javascript - DOM 元素 addclass 在 IE7 中不起作用

mysql - 为什么 mysql INSERT ... ON DUPLICATE KEY UPDATE 会破坏主/主配置上的 RBR 复制

java - 无法从 Servlet 连接数据库

java - 删除 H2 数据而不删除表

javascript - 将 BeforeUnload 缩小为仅在字段更改或更新时触发

mysql - 如何在jpql查询中动态连接表

java - 仅根据用户输入的一部分创建字符串

java - Groovy 方法重载 : selection of method prefers interfaces over subclasses?

php - sql插入语句有时工作其他时间不工作