java는 여러 개의 파일을 동적으로 업로드하고 파일 이름 바꾸기 문제를 해결합니다
1. 자바는 여러 개의 파일을 동적으로 업로드합니다.
2. 파일 이름 바꾸기 문제 해결
여러분의 참고를 제공하며, 구체적인 내용은 아래와 같습니다.
1. 여러 파일을 동적으로 업로드
<form name="xx" action="<c:url value='/Up3Servlet'/>" method="post" enctype="multipart/form-data">
<table id="tb" border="1">
<tr>
<td>
File:
</td>
<td>
<input type="file" name="file">
<button onclick="_del(this);"> </button>
</td>
</tr>
</table>
<br/>
<input type="button" onclick="_submit();" value=" ">
<input onclick="_add();" type="button" value=" ">
</form>
</body>
<script type="text/javascript">
function _add(){
var tb = document.getElementById("tb");
//
var tr = tb.insertRow();
//
var td = tr.insertCell();
//
td.innerHTML="File:";
// td
var td2 = tr.insertCell();
// input
td2.innerHTML='<input type="file" name="file"/><button onclick="_del(this);"> </button>';
}
function _del(btn){
var tr = btn.parentNode.parentNode;
//alert(tr.tagName);
// tr table
var index = tr.rowIndex;
//
var tb = document.getElementById("tb");
tb.deleteRow(index);
}
function _submit(){
//
var files = document.getElementsByName("file");
if(files.length==0){
alert(" ");
return false;
}
for(var i=0;i<files.length;i++){
if(files[i].value==""){
alert(" "+(i+1)+" ");
return false;
}
}
document.forms['xx'].submit();
}
</script>
</html>
업로드할 모든 파일을 훑어보다2. 파일의 이름 변경 문제 해결
package cn.hx.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
public class UpImgServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String path = getServletContext().getRealPath("/up");
DiskFileItemFactory disk =
new DiskFileItemFactory(1024*10,new File("d:/a"));
ServletFileUpload up = new ServletFileUpload(disk);
try{
List<FileItem> list = up.parseRequest(request);
// *.jpg-iamge/jpege.,bmp/imge/bmp,png,
List<String> imgs = new ArrayList<String>();
for(FileItem file :list){
if(file.getContentType().contains("image/")){
String fileName = file.getName();
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
//
String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
//UUID
String uuid = UUID.randomUUID().toString().replace("-", "");
//
String newName = uuid+extName; // UUID ,
FileUtils.copyInputStreamToFile(file.getInputStream(),
new File(path+"/"+newName));
// list
imgs.add(newName);
}
file.delete();
}
request.setAttribute("imgs",imgs);
request.getRequestDispatcher("/jsps/imgs.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
}
}
이상의 자바 다중 파일 업로드를 실현하고 파일 이름 변경 문제를 해결하여 여러분의 학습에 도움이 되기를 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.