你好,我试图在java中使用客户端 - 服务器类发送文件.出于某种原因,当调用发送文件的方法时,套接字关闭.这是代码:
FileInputStream fIn = new FileInputStream(file);
out = new BufferedOutputStream(clientSocket.getOutputStream());
byte fileContent[] = new byte[(int) file.length()];
fIn.read(fileContent);
for (byte b : fileContent) {
out.write(b);
}
Run Code Online (Sandbox Code Playgroud)
和来自客户端的代码:
FileOutputStream fIn = new FileOutputStream("testing");
BufferedInputStream inAout = new BufferedInputStream(clientSocket.getInputStream());
byte fileContent[] = new byte[1000000];
inAout.read(fileContent);
fIn.write(fileContent);
Run Code Online (Sandbox Code Playgroud)
和我得到的错误消息:SEVERE:null java.net.SocketException:Socket关闭
我真的没有经验,所以如果有任何可以帮助它将是伟大的.
我有两个问题的图标.
我使用ImageIcons作为JTree上的默认图标.正如教程所建议的那样,我使用这种方法加载图标:
protected ImageIcon createImageIcon(String path) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然为了实现这一点,我必须使用我认为很乱的源代码将图像存储在文件中(但我阅读了运行.jar所需的内容).有没有办法在项目文件夹而不是src上加载图像?
还有一种方法可以自动缩放正确大小的图像以便在JTree上显示吗?谢谢 !
我正在制作类似于聊天的应用程序.为此,我有两个JTextPanes,一个是我正在编写的,一个是显示消息的.这是处理从输入到显示的文本传输的代码:
String input = textPane.getText();
if(!input.endsWith("\n")){
input+="\n";
}
StyledDocument doc = displayPane.getStyledDocument();
int offset = displayPane.getCaretPosition();
textPane.setText("");
try {
doc.insertString(offset, input, set);
} catch (BadLocationException ex) {
Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我在输入文本的某些单词上有颜色,则输出都是彩色的.因此,当移动到显示时(在输入时正确显示),颜色将应用于所有文本.关于如何正确移动文本的任何建议?
请注意,其他格式与粗体,斜体等相同
我希望在我身上有这样的方法 DocumentFilter
public void replaceUpdate(int offset, int length, String text) {
try {
super.replace(byPass, offset, length, text, null);
} catch (BadLocationException ex) {
//error
}
}
Run Code Online (Sandbox Code Playgroud)
目前为了获得FilterBypass的实例(上面的方法中的byPass),我需要从重写的方法insertString获取:
private FilterBypass byPass;
@Override
public void insertString(DocumentFilter.FilterBypass fb,
int offset, String string, AttributeSet att)
throws BadLocationException {
byPass = fb;
//some stuff here
super.insertString(fb, offset, string, att);
}
Run Code Online (Sandbox Code Playgroud)
但这给我带来了一些问题.任何人都可以建议一些不同的方式获得FilterBypass?我无法找到一种方法来获得FilterBypass不同的参考.
如果我要覆盖它的方法应该怎么做?
我有以下目录
/root
/app
/api
my_api.py
/service
my_service.py
/tests
test_api.py
Run Code Online (Sandbox Code Playgroud)
my_api.py
import app
def run_service():
app.service.my_service.service_function()
Run Code Online (Sandbox Code Playgroud)
test_api.py
@patch('app.service.my_service.service_function')
test_run_service(self,mock_service):
mock_service.return_value = 'Mock'
response = self.client.get(url_for('api.run_service')
self.assertTrue(response == expected_responce)
Run Code Online (Sandbox Code Playgroud)
以上工作.我无法弄清楚,我需要修补哪个模块,以防我想service_function在my_apy.py中导入,如下所示:
from app.service.my_service import service_function
Run Code Online (Sandbox Code Playgroud)
如果我像上面那样进行导入,模拟停止工作.
给定一个最初从 Spotify Android/iOS SDK 收到的身份验证令牌,我想向 Spotify 核实我持有的令牌是否有效。这样做是否有一个最小的终点?
理想情况下不是数据密集型的,因为我只想知道令牌是否有效。这必须使用 Web API 而不是 SDK 来完成。
是否可以使用switch并具有不同的子类?我想使用switch而不是几个if语句.我有一个名为指令的抽象类和具有不同类型的子类.所以我希望每个案例都是一个不同的子类.喜欢 :
switch (instruction){
case instruction1:
case instruction2:
case instruction3:
}
Run Code Online (Sandbox Code Playgroud)
等,所述指令1,2,3是同一类的子类.
我需要一种方法只删除字符串中找到的第一个空格,然后将字符串放在一个数组中.例如
hello there. Hey.
Run Code Online (Sandbox Code Playgroud)
我希望它像分裂一样[hello][there. Hey].我试过了
String [] s = str.split(" ")
Run Code Online (Sandbox Code Playgroud)
通过它将自然地删除所有空格并创建几个字符串.我只需要2.你能告诉我怎么做吗?通过正则表达或其他方式的以太.
如何限制组件占用的空间,以便在显示时只占用所需的空间?当我添加到面板时,我会得到奇怪的大按钮和东西.
我有一个带有Android模块的ReactNative项目,用于使用Spotify SDK进行身份验证,稍后将使用回放.
在构建项目时,来自grandle的错误如下:
> Duplicate files copied in APK lib/armeabi-v7a/libgnustl_shared.so
File 1: ...root../android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.16.1/jni/armeabi-v7a/libgnustl_shared.so
File 2: ...root../android/app/build/intermediates/exploded-aar/com.spotify.sdk/spotify-player/1.0.0-beta12/jni/armeabi-v7a/libgnustl_shared.so
Run Code Online (Sandbox Code Playgroud)
使用packagingOptionsexclude或pickFirst打破了应用程序,因为两个库看起来不同(一个几乎是大小的两倍)但具有相同的名称.
有什么选择可以解决这个问题?
我试图在多线程应用程序上应用状态模式.问题是编译器不喜欢我的类扩展Thread的事实.这是代码:
public class ConnectionHandler extends Thread
private State lockedState;
public ConnectionHandler(Socket socket){
...
lockedState = new LockedState(this);
}
public State getState(){}
public void setState(State state){}
{
public interface State {
public void PASSWD(String pass);
public void ACCESS(String file);
public void getDIR();
public void QUIT();
}
public class LockedState implements State {
ConnectionHandler connectionHandler;
public LockedState(ConnectionHandler handler){
connectionHandler=handler;
}
public void PASSWD(String pass){
public void ACCESS(String file){}
public void getDIR(){}
public void QUIT(){}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是ConnectionHandler的构造函数:找不到兼容的类型java.lang.Thread.State:Networks2.LockedState.当我从Connectionhandler中删除extends Thread时,它不会给出任何错误,但那不是一个选项.所以问题是:我该怎么做才能让编译器不抱怨?谢谢
有几次我遇到过for-each循环会导致问题的情况,包括异常和崩溃,而for(it=list.iterator;it.hasNext();)没有任何问题.这包括修改集合(我知道不应该为每个集合发生,但不知道为什么)以及我克隆东西的其他情况.不记得任何具体的例子,我只是想到了它.
for-each只是我指向的第二个循环类型的快捷方式吗?有人可以解释那里的差异究竟是什么?
我一直在努力通过一个简单的 Java 应用程序来执行命令。我需要执行的命令在http://www.imagemagick.org/Usage/annotating/#wmark_text(标题为“带图像的水印”)中找到,如下所示:
convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'Copyright'" -gravity SouthEast -draw "text 5,15 'Copyright'" miff:- | composite -tile - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg
Run Code Online (Sandbox Code Playgroud)
(我已经通过直接将其输入终端来测试该命令并且它有效)
我无法正确创建命令。使用我的代码构造以下命令:
convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'Copyright" -gravity SouthEast -draw "text 5,15 'Copyright" miff:- | composite -title - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg
Run Code Online (Sandbox Code Playgroud)
此外,还会返回许多与图像魔法相关的错误:
convert: non-conforming drawing primitive definition `text 10,10 'Copyright' @ error/draw.c/DrawImage/3178.
convert: non-conforming drawing primitive definition `text 5,15 'Copyright' @ error/draw.c/DrawImage/3178.
convert: unable …Run Code Online (Sandbox Code Playgroud) java ×10
swing ×4
spotify ×2
command-line ×1
gradle ×1
icons ×1
imagemagick ×1
jtextpane ×1
loops ×1
mocking ×1
networking ×1
python ×1
python-3.4 ×1
react-native ×1
regex ×1
sockets ×1
string ×1
unit-testing ×1