S_Servlet完成

hechu1
zhai_lw 6 years ago
parent 2bd6e8c4c9
commit 8a5678029c

@ -14,18 +14,12 @@ import java.util.Map;
public class UploadFile extends Operation {
@Override
public Map<String, Object> execute(User subject) throws Exception {
File file = (File) this.getOptions().get("file");
String table = (String) this.getOptions().get("file_type");
Map<String,String> limits = (Map<String, String>) this.getOptions().get("limits");
String field = core.operation.utils.Utils.getFileField(table);
String filePath = core.operation.utils.Utils.getFilePathName(table,file.getName());
String filePath = (String) this.getOptions().get("file_url");
Map<String,String> vMap = new HashMap<>();
vMap.put(field,filePath);
file.renameTo(new File(filePath));
//todo
List<String> ls = new ArrayList<>();
ls.add(field);
List<String[]> resultLs = DBManagement.select(ls,table,limits,0,1);

@ -1,6 +1,6 @@
{
"options":{
"file": "File",
"file_url": "String",
"file_type": "String",
"limits":"Map<String,String>"
},

@ -35,19 +35,4 @@ public interface Utils {
static String getFileField(String table){
return fileTableField.get(table);
}
static String getFilePath(String table){
return Configuration.filePath+table+"/";
}
static String getFileName(String fileName){
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyMMddHHmmssSSS");
Date date = new Date();
String str = simpleDateFormat.format(date);
str+=(int)(Math.random()*100000);
str+= utils.Utils.getFileType(fileName);
return str;
}
static String getFilePathName(String table, String fileName){
return getFilePath(table)+getFileName(fileName);
}
}

@ -1,7 +1,5 @@
package servlet;
import core.user.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@ -8,10 +8,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "student-upload-finished-product")
@WebServlet("/student-upload-finished-product")
@MultipartConfig
public class S_StudentUploadFinishedProduct extends HttpServlet {
static String fileType = "graduation_design_finished_product";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String fileURL = servlet.Utils.saveFile(request, fileType);
}
}

@ -14,30 +14,10 @@ import java.util.UUID;
@WebServlet("/student-upload-opening-report")
@MultipartConfig
//学生id+文件
public class S_StudentUploadOpeningReport extends HttpServlet {
private static final long serialVersionUID = 1L;
static String fileType = "graduation_design_opening_report";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
User user=(User) request.getSession().getAttribute("user");
String student_id=user.getId();
try {
//获取上传的文件
Part part=request.getPart("file");
//获取请求的信息
String name=part.getHeader("content-disposition");
//获取上传文件的目录
String root=request.getServletContext().getRealPath("/upload");
request.setAttribute("info", "上传文件成功");
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("info", "上传文件失败");
}
request.getRequestDispatcher("/upload.jsp").forward(request, response);
String id = request.getParameter("id");
String fileURL = servlet.Utils.saveFile(request, fileType);
}
}

@ -1,8 +1,17 @@
package servlet;
import gdms.Configuration;
import org.apache.coyote.Request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -15,4 +24,37 @@ public interface Utils {
}
return vMap;
}
static String getFilePath(String table){
return Configuration.filePath+table+"/";
}
static String getFileName(String fileName){
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyMMddHHmmssSSS");
Date date = new Date();
String str = simpleDateFormat.format(date);
str+=(int)(Math.random()*100000);
str+= utils.Utils.getFileType(fileName);
return str;
}
static String getFilePathName(String table, String fileName){
return getFilePath(table)+getFileName(fileName);
}
static String saveFile(HttpServletRequest request, String table) throws IOException, ServletException {
Part part = request.getPart("graduation_design");
String header = part.getHeader("content-disposition");
String path = header.substring(header.indexOf("filename=") + 10, header.length() - 1);
String filePathName = servlet.Utils.getFilePathName(table,path);
File file = new File(filePathName);
InputStream inputStream = part.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
int len;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
outputStream.close();
inputStream.close();
part.delete();
return filePathName;
}
}

Loading…
Cancel
Save