我正在写一个文件上传程序在spring mvc 3
这是我的控制器类
package com.fss;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@Controller
public class FileController {
@Autowired
ServletContext context;
private static final String PATH="D:\\fdirct";
public int isDuplicate(String file)
{
int i=0;
File f = new File(PATH);
String[] listOfFiles = f.list();
for (int k = 0; k < listOfFiles.length; k++) {
if( file.equals(listOfFiles[k]))
{
i=1;
}
}
return i;
}
@RequestMapping("/toupload")
public ModelAndView uploadFile(){
return new ModelAndView("fileUpload");
}
//uploading if not existing file
@RequestMapping(value="savefile",method=RequestMethod.POST)
public ModelAndView uploadFile( @RequestParam CommonsMultipartFile file) throws Exception{
int c=0;
byte[] bytes = file.getBytes();
String filename = file.getOriginalFilename();
String message ="";
String x="";
if(file.isEmpty())
{
message="please select a file to upload ";
ModelAndView mav=new ModelAndView("fileUpload","message",message);
return mav;
}
else
{
//to check whether the file already exists
c=isDuplicate(filename);
if(c==1)
{
x="crosscheck";
}
else if(c==0)
{
x="fileUpload";
}
ModelAndView mav=new ModelAndView(x);
//if file already exists
if(c==1)
{
bytes.toString();
String filecontent=new String(bytes);
mav.addObject("filename",filename);
mav.addObject("filecontent",filecontent);
mav.addObject("1",c);
message="File already exists !! Do you want me to overwrite it ?";
}
//if file doesn't exists
else if(c==0)
{
File f1=new File(PATH + File.separator + filename);
FileOutputStream fos=new FileOutputStream(f1);
BufferedOutputStream bos =new BufferedOutputStream(fos);
bos.write(bytes);
bos.flush();
bos.close();
message="File successfully uploaded ";
}
mav.addObject("message", message);
return mav;
}
}
//crosschecking if exisitng file
@RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(@ModelAttribute("choice") Choice choice) throws IOException{
String ch="";
String m="";
ch=choice.getChoice();
if(ch.equalsIgnoreCase("no"))
{
m="Upload suspended as requested by user ";
}
else if(ch.equalsIgnoreCase("yes"))
{
//System.out.println("In case of YES ");
String filename=choice.getFname();
String content=choice.getFcontent();
File f1=new File(PATH+File.separator+filename);
FileWriter fw=new FileWriter(f1);
fw.write(content);
fw.close();
m ="File successfully uploaded by overwriting the existing file !";
}
return new ModelAndView("crosscheck","m",m);
}
}
在上面的代码中,我正在检查正在上传的文件是否是现有文件。如果没有,我上传文件。如果它是现有文件,我将控件重定向到jsp页面,要求用户覆盖该文件,并以html格式收集用户选择。
我使用的jsp页面
fileUpload.jsp
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form"%>
<%@ 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>File Upload Example</title>
</head>
<body style="background-color:powderblue;color:Tomato">
<br>
<br>
<center><form:form action="savefile" method = "POST" modelAttribute = "fileUpload"
enctype = "multipart/form-data">
Please select a file to upload :
<input type = "file" name = "file" /><br><br><br>
<input type = "submit" value = "upload" />
<input type = "reset" value = "Reset" />
</form:form></center>
<br>
<br>
<br>
<center><b> ${message} </b></center>
<br>
<center> <a href="view">View files</a><br><br>
<a href="uploadxml">Upload xml as text file</a> <br><br>
<a href="uploadcsv">Upload CSV as text file</a> <br><br>
<a href="uploadxls">Upload excel as text file</a> <br><br>
<a href="deletefiles">Delete multiple files</a>
</center>
</body>
</html>
crosscheck.jsp
<%@ 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>Insert title here</title>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain">
<center><h3>${message}(Say yes\no)</h3></center>
<br>
<input type="text" name="choice">
<br><br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}">
<input type="submit" value="submit" /> </form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center><a href="toupload">Click me to upload a file</a> <br><br>
<a href="uploadxml">Upload xml as text file</a> <br><br>
<a href="uploadcsv">Upload CSV as text file</a> <br><br>
<a href="uploadxls">Upload excel as text file</a> <br><br>
<a href="view">View files</a><br><br>
<a href="deletefiles">Delete multiple files</a></center>
</body>
</html>
如何使用javascript确认框而不是使用html表单来了解用户的选择?如果用户选择OK,则应覆盖文件,否则应显示“Uploading suspended”消息。如何以及在哪里写确认框?
这是我的bean类
package com.fss;
public class Choice {
private boolean choice;
private String fname;
private String fcontent;
public boolean isChoice() {
return choice;
}
public void setChoice(boolean choice) {
this.choice = choice;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFcontent() {
return fcontent;
}
public void setFcontent(String fcontent) {
this.fcontent = fcontent;
}
}
我包括我的jsp页面。
<%@ 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>Insert title here</title>
<script>
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
return k;
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="button" value="Overwrite ?" onclick="choice=getChoice()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center><a href="toupload">Click me to upload a file</a> <br><br>
<a href="uploadxml">Upload xml as text file</a> <br><br>
<a href="uploadcsv">Upload CSV as text file</a> <br><br>
<a href="uploadxls">Upload excel as text file</a> <br><br>
<a href="view">View files</a><br><br>
<a href="deletefiles">Delete multiple files</a></center>
</body>
</html>
如何将bean变量“choice”设置为Confirm弹出窗口的结果?
答案 0 :(得分:0)
试试这个
function myFunction() {
var a = confirm("Press a button!");
console.log(a)
}
<button onclick="myFunction()">Try it</button>